How can you access scala.None
from Java?
The last line causes the compiler to die with "type scala.None does not take parameters".
import scala.Option;
import scala.Some;
import scala.None;
final Option<String> object1 = new Some<String>("Hi there");
final Option<String> object2 = new None<String>();
This fails with "cannot find symbol constructor None()":
final Option<String> object2 = new None();
This fails with "cannot find symbol variable None":
final Option<String> object2 = None;
In 2007 this used to work, but then Scala changed. The Java compiler gives error: incompatible types
:
final Option<String> object2 = scala.None$.MODULE$;
This might work:
final scala.Option<String> x = scala.Option.apply(null);
def apply [A] (x: A): Option[A]
An Option factory which creates Some(x) if the argument is not null, and None if it is null.
Using Scala 2.10.2 (not sure at what version when this came in):
final Option<String> none = Option.empty();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With