Possible Duplicate:
Accessing scala.None from Java
In Java you can create an instance of Some
using the constructor, i.e. new Some(value)
, but None
has no partner class. How do you pass None
to a Scala function from Java?
Possible Duplicate:
Accessing scala.None from Java
In Java you can create an instance of Some
using the constructor, i.e. new Some(value)
, but None
has no partner class. How do you pass None
to a Scala function from Java?
You can access the singleton None instance from java using:
The
scala.None$.MODULE$
thing doesn't always typecheck, for example this doesn't compile:because javac doesn't know about Scala's declaration-site variance, so you get:
This does compile, though:
so that's a different way to get a None that is usable in more situations.
I've found this this generic function to be the most robust. You need to supply the type parameter, but the cast only appears once, which is nice. Some of the other solutions will not work in various scenarios, as your Java compiler may inform you.
I think this ugly bit will work:
scala.None$.MODULE$
There is no need for a new instance since one None is as good as another...
Faced with this stinkfest, my usual modus operandi is:
Scala:
Java: