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?
+10
A:
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...
Mitch Blevins
2010-01-04 04:42:33
+8
A:
You can access the singleton None instance from java using:
scala.None$.MODULE$
Randall Schulz
2010-01-04 04:46:27
A:
The scala.None$.MODULE$
thing doesn't always typecheck, for example this doesn't compile:
scala.Option<String> x = scala.None$.MODULE$;
because javac doesn't know about Scala's declaration-site variance, so you get:
J.java:3: incompatible types
found : scala.None$
required: scala.Option<java.lang.String>
scala.Option<String> x = scala.None$.MODULE$ ;
This does compile, though:
scala.Option<String> x = scala.Option.apply(null);
so that's a different way get a None that is usable in more situations.
Seth Tisue
2010-09-15 17:12:55
A:
Faced with this stinkfest, my usual modus operandi is:
Scala:
object SomeScalaObject {
def it = this
}
Java:
doStuff(SomeScalaObject.it());
Alex Cruise
2010-09-15 17:30:10