Accessing scala.None from Java

Mike Slinn picture Mike Slinn · Jan 30, 2012 · Viewed 8.2k times · Source

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$;

Answer

om-nom-nom picture om-nom-nom · Jan 30, 2012

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.