Java 7 introduced the concept of method handles in the language, which are
somewhat like function pointers in C++, except more type-safe (like Java)
and with a more wordy API (also like Java).

The current version of srborlongan-picolisp (essentially mainline PicoLisp
with changes located in the ersatz/ and java/) now uses method handles on
Java imports if Ersatz in run in a Java 7+ VM (Java 6 uses still use the
old (last week) import code).

Now, when a user invokes, say, String.format, instead of calling (java
"java.lang.String" "format" "%s%s" (list "1" "2") ), where the list must
not contain primitives, users may now use (String.format "%d%d%d" 1 2 3),
wherein the varargs property of the original method (String::format(String,
Object...) is maintained by the corresponding method handle, which is then
used internally by all imported Java functions.

Thus, we now have the ability to call Java functions that previously
dependended on all-Object arguments fluently:

(java "java.util.Arrays" "asList" (1 2 3) ) # does not work
(java+ "java.util.Arrays" "asList" (1 2 3) ) # still does not work
(java "java.util.Arrays" "asList" (listAsArray (1 2 3) ) ) # uses method
handles, works, but without varargs functionality of the original
(Arrays.asList 1 2 3) $ works!

This feature was inspired by the implementation of the cache method. In
particular, the idea of binding a symbol in the function itself that may
serves as an effectively private static variable (in Java-speak) if said
symbol is transient is rather, well, beautiful.

Samuel Dennis R. Borlongan

Reply via email to