(rontolisp) docs
← Functions

java:proxy

(java:proxy "fully.qualified.Interface" callable)

Creates a host instance of the given interface backed by a rontolisp callable. Every interface method is dispatched to the callable as (callable "method-name" arg...) — so the callable's first argument is the name of the invoked method (a string) and the remaining arguments are the method's arguments. The callable's return value is marshalled back to the method's return type (a void method ignores it). This is how a rontolisp lambda becomes a Java listener or comparator. For a single-method (SAM) interface the method name is always the same, so it is conventionally ignored (the method parameter in the examples below). Part of the JVM-only java interop package — available on the interpreter and in JVM-compiled classes, not on the WASM backend. See the Java interop guide.

A java.util.function.Supplier is implemented by the lambda; calling its get method runs the lambda and returns 42.

Functional interfaces

Any interface works, including the java.util.function family. The lambda's first parameter receives the method name; the rest receive the method arguments, so match the arity to the interface's single abstract method (SAM):

InterfaceSAMLambda shape
Supplierget()(lambda (method) ...)
Functionapply(x)(lambda (method x) ...)
Consumeraccept(x)(lambda (method x) ...)
Predicatetest(x)(lambda (method x) ...)
BiFunctionapply(a, b)(lambda (method a b) ...)
BinaryOperatorapply(a, b)(lambda (method a b) ...)
Comparatorcompare(a, b)(lambda (method a b) ...)

The proxy also works when the JDK itself invokes the SAM method. For example HashMap.merge calls the supplied BiFunction to combine the old and new value:

Default methods

A dynamic proxy routes every method call to the callable, including default methods such as BiFunction.andThen or Predicate.and. Calling one dispatches to the lambda as (callable "andThen" ...) rather than running the interface's built-in default implementation, so combinators like (f.andThen g) are not available — call the single abstract method (apply/test/accept/get/ compare) instead.