java - Can I return an anonymous closure in JRuby when an interface is expected? -
per the docs, can pass block method expects interface, , block converted anonymous implementation of interface. it's pretty cool!
however, have ruby subclass of java class, has method that's supposed return implements interface.
// java public abstract class parent { public myinterface getit(); }
# ruby class child < parent def getit # need return implementation of myinterface? end end
i have tried making proc
, lambda
, neither of work. can add method parent (in java) forces automatic conversion
protected myinterface passthrough(myinterface mi) { return mi; }
and works:
def getit passthrough {|thing| puts thing.inspect } end
this seems hacky though. oversight in how jruby works, or missing alternate solution?
yes - should able ... use to_java
on proc/lambda :
class child < parent def getit lambda { |*args| puts args.inspect }.to_java(java::myinterface) end end
Comments
Post a Comment