newInstance

Synopsis

$ newInstance(^Class|@array, &closure)

Creates an instance of the specified Java interface (or interfaces if an array is used) backed by the specified closure.

Parameters

^Class - the class to create an instance of (limited to Java interfaces for now)

@array - an array of Java classes to create an instance of

&closure - the closure to back this proxy Java object with.

Returns

an instance of a Java object that implements the specified interfaces.

Side Effects / Notes

Examples

@list = @("a", "b", "c", "d", "e"); sub iterator { if ($0 eq "hasNext") { return size(@data); } if ($0 eq "next") { return shift(@data); } } $iter = newInstance(^java.util.Iterator, lambda(&iterator, @data => @list)); while ([$iter hasNext]) { $element = [$iter next]; println($element); }

a b c d e

See Also