Mike Slinn Mike Slinn - 4 months ago 14
Scala Question

Accessing scala.None from Java

How can you access

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.