Node:Method operations, Next:Field operations, Previous:Allocating objects, Up:Objects Classes and Modules
Kawa has both a low-level and a high-level "Foreign Function Interface", which allows you to call any (virtual or static) Java method as if it were a Scheme procedure.
invoke-static class name args ... | Function |
The class can be a <java.lang.Class> , a
<gnu.bytecode.ClassType> , or a <symbol> or <string>
that names a Java class. The name can be <symbol> or
<string> that names one or more methods in the Java class.
The name is "mangled" (see Mangling) into a valid Java name.
Any public methods (static or instance) in the specified class (or its
super-classes) that match "name" or "name$V" collectively form a
generic procedure. When the procedure is applied to the argument list,
the most specific applicable method is chosen depending on the
argument list; that method is then
called with the given arguments. Iff the method is an instance method,
the first actual argument is used as the ("name$V" is used for procedures with An example (derived from the Skij FAQ):
(invoke-static <java.lang.Thread> 'sleep 100) The behavior of interpreted code and compiled code is not
indentical, though you should get the same result either way
unless you have designed the classes rather strangely. The
details will be nailed down later, but the basic idea is that
the compiler will "inline" the |
invoke object name args ... | Function |
The name can be <symbol> or
<string> that names one or more methods in the Java class.
The name is "mangled" (see Mangling) into a valid Java name.
Any public methods (static or instance) in the specified class (or its
super-classes) that match "name" or "name$V" collectively form a
generic procedure. When the procedure is applied to the argument list,
the most specific applicable method is chosen depending on the
argument list; that method is then
called with the given arguments. Iff the method is an instance method,
the object is used as the ("name$V" is used for procedures with The behavior of interpreted code and compiled code is not
indentical, though you should get the same result either way
unless you have designed the classes rather strangely. The
details will be nailed down later, but the basic idea is that
the compiler will "inline" the If the compiler cannot determine the method to call (assuming
the method name is constant), the compiler has to generate code
at run-time to find the correct method. This is much slower,
so the compiler will print a warning. To avoid a waning, you can
use a type declaration, or insert a cast:
(invoke (as <java.util.Date> my-date) 'setDate cur-date) |
class-methods class name | Function |
Return a generic function containing those methods of class
that match the name name, in the sense of invoke-static .
Same as:
(lambda args (apply invoke-static (cons class (cons name args)))) |
Some examples using these functions are vectors.scm
and characters.scm
the directory kawa/lib
in
the Kawa sources.