Hi Alex,

Is the "update" below what's available on Ersatz documentation? I couldn't find this info anywhere else. I think it would have been nice to have a link to this kind of Ersatz documentation from the wiki.


On 11/4/10 7:53 PM, Alexander Burger wrote:
Hi all,

here is an update.

Besides the basic I/O system and half-baked network functions (until now
only 'connect', 'accept' and a partial 'port'), we have now a set of
functions interfacing to the Java Reflection API.

I'd like to describe them shortly, and hear your opinions. You can try
it if you like if you download the current testing release.

The central function is called 'java'. It comes in three forms:

(java 'cls 'T 'any ..) ->  obj
    This is a constructor call, returning a Java object. 'cls' is the
    name of a Java class, like "java.lang.StringBuilder".

(java 'cls 'msg 'any ..) ->  obj
    This calls a static method 'msg' for a class 'cls'.

(java 'obj 'msg 'any ..) ->  obj
    This calls a dynamic method 'msg' for an object 'obj'


    : (setq Sb (java "java.lang.StringBuilder" T "abc"))
    ->  $StringBuilder
    : (java Sb "append" (char: 44))
    ->  $StringBuilder
    : (java Sb "append" 123)
    ->  $StringBuilder
    : (java Sb "toString")
    ->  $String
    : (data @)
    ->  "abc,123"

The function 'data' in the last line converts back from Java objects to
PicoLisp data.

Then we have 'public', it supports two forms:

    (public 'obj 'any) ->  obj
       Returns the value of a public field 'any' in object 'obj'

    (public 'cls 'any) ->  obj
       Returns the value of a public field 'any' in class 'cls'


    : (public "java.lang.System" "err")
    ->  $PrintStream
    : (java @ "println" "Hello world")
    Hello world
    ->  NIL

Finally, we have a set of type conversion functions, to produce Java
objects from Lisp data:

    (byte: 'num|sym) ->  obj
    (char: 'num|sym) ->  obj
    (int: 'num) ->  obj
    (long: 'num) ->  obj
    (big: 'num) ->  obj

We saw the usage of 'char:' in the StringBuilder example above.

So the 'any' arguments to the 'java' function can either be
    - 'T' or 'NIL', then the type is boolean
    - A number, then it must fit in 32 bits and will be passed as 'int'
    - A normal symbol, then it will be passed as 'String'
    - A Java object, as returned by 'java' or one of the 'xxx:'
      conversion functions.
    - A list, then all elements must be of the same type and will be
      passed as 'Array'

I hope this reflection interface is a good compromise between simplicity
and usefulness.

- Alex

UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to