RingJavafor Ring

Rules and limits

Ring is a small dynamic language and Java is a large static one.

Ring is a small dynamic language and Java is a large static one. These are the rules that come from the difference. Each has a way around it.

Ring needs the exact number of values#

Ring has no default values and no overloading: f(1) for func f a,b is an error ("less number of parameters"), and so is f(1,2,3). So a Ring method has one number of parameters. See Core concepts: a Java name that has a form without parameters and forms with parameters is two Ring methods, name() and @name(x); @name(x) takes one argument (a hash list of :name = value pairs, a value or a list of values). A Java name with several numbers of parameters and none without is one Ring method with one argument. callMethod("name",[args]) takes any.

new StringBuilder() (empty parentheses) is an error for a class whose init takes values: write new StringBuilder or new StringBuilder("x").

Ring keywords#

get, put, next, end, new, load, class, import, call, exit, return, if, for, do, init... cannot follow a dot (to, from, step, in, set can). A Java method or field with one of these names starts with @: oMap.@get("k"), oMap.@put("k",1), oIterator.@next(). The generator's list is aJavaKeywords in libraries/javalib/globals.ring.

Ring is case insensitive#

oList.add and oList.ADD are the same method. Java methods that differ only in case (rare) are one Ring method; the bridge picks the exact name first.

Static classes#

Ring has no static calls on a class name, so RingJava makes objects of the classes. The classes of java.lang are ready as global objects: Math.max(3,9), System.currentTimeMillis(), Integer.parseInt("12"), String.valueOf(5). For another class make the object yourself (Arrays = new Arrays, oMath = new Math) or use jStatic("java.util.Arrays","sort",[aItems]). new String("abc") makes a Java String object.

Statements before classes and functions#

In a Ring file, the code of the program comes first and the func and class definitions after it. A function that Java calls (a callback) is defined at the end of the file.

Numbers#

Ring numbers are doubles. A whole number goes to Java as int (or long when it does not fit), a number with a fraction as double. long values above 2^53 lose precision in Ring: use jLong and BigInteger / BigDecimal (jBigInteger("123456789012345678901234567890")) to keep them exact. A long that Java returns is a Ring number (a double).

Objects#

Threads#

Ring runs on one thread. Java threads (a Thread, an executor, a timer, the Swing event thread) can call Ring callbacks only while the Ring program runs jPump(), jSleep(), jJoin() or jWaitUntil(); a callback of another thread that is not served in time (30 seconds, jCallbackTimeout(ms)) gets an error. Blocking Java calls (thread.join(), future.get(), latch.await()) block the Ring program: use the helper that pumps, or poll with isDone(). See Callbacks and threads.

One JVM for the process#

The JVM starts at the first call (or jStart()), cannot be restarted, and the JDK folder and the JVM options (jUseJDK, jOptions) must be set before. A program that starts non-daemon Java threads (a Swing window, an executor that is not shut down) keeps the process alive only if the Ring program waits; when Ring exits, the process exits.

JDK modules#

Java 9+ keeps the internals of the JDK private. The public classes of the exported packages work. Deep reflection into JDK internals fails with InaccessibleObjectException; open a package with jOptions(["--add-opens","java.base/java.lang=ALL-UNNAMED"]) when you really need it.

RingJava - use Java classes and methods from Ring programs.