Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing scala.None from Java

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$;
like image 917
Mike Slinn Avatar asked Jan 30 '12 04:01

Mike Slinn


2 Answers

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.

like image 191
om-nom-nom Avatar answered Nov 15 '22 16:11

om-nom-nom


Using Scala 2.10.2 (not sure at what version when this came in):

final Option<String> none = Option.empty();
like image 18
Dave Moten Avatar answered Nov 15 '22 16:11

Dave Moten