Scala and Java interop. of Future -
in problem have call third-party java library expects java.util.concurrent.future result scala routine returning scala.concurrent.future example.
def somescalafunction(): scala.concurrent.future[t] def otherjavamethod(arg: java.util.concurrent.future[t]) = ... i wrap scala-future new java-future, there no way implement java-future method def cancel(mayinterruptifrunning: boolean): boolean in way interrupts wrapped scala-future (if otherwise please let me know).
how can solve problem? these approaches come with:
- write wrapper anyway , ignore call on
cancel(or thrownotimplementederror) - change
somescalafunctionmaybe return closure wrapped caller in scala or java future.
the problem 1. client rely on proper implementation of cancel maybe not critical. 2. result in ugly api.
note cancel attempts cancel task, there no guarantees according javadoc. don't have implement -- depending on future computation is, ignoring cancel call may not subsequential entire application.
if need cancel it, see this question half-solution.
otherwise, use second solution -- of implicit conversions make nicer.
object javainterop { implicit def f2future[f[_]](f: () => t): java.util.concurrent.future[t] = fc(f) } this way, import controls conversion want provide , when.
one issue might conversion don't want -- might yield surprising effects.
Comments
Post a Comment