29

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$;
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85

6 Answers6

36

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
18

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

final Option<String> none = Option.empty();
Dave Moten
  • 11,957
  • 2
  • 40
  • 47
9

Not to put too fine a point on it, but one shouldn't rely on static forwarders, as they might not get generated under certain circumstances. For instance, Some doesn't have static forwarders for its object companion.

The current way of accessing methods on an object companion is this:

final scala.Option<String> x = scala.Option$.MODULE$.apply(null);

Option$ is the class for the object companion of Option. MODULE$ is a final public static field that contains the sole instance of Option$ (in other words, the object companion).

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
1

You can import

import scala.compat.java8.OptionConverters;

And then:

OptionConverters.<YourType>toScala(Optional.empty())
Christian Vielma
  • 15,263
  • 12
  • 53
  • 60
0

Using Scala version 2.11.4 the only thing that worked out in my case, was

Option<String> maybeAmount = (amount != MISSING_STOCKAMOUNT) 
? Some.apply(amount+"") : Option.<String>apply(null); // can you feel the pain??

Update: a day later...

Option.apply(null);

without the type parameter does work too, as it should. For some reason I couldn't get Intellij to compile the code on my first attempt. I had to pass the secondary type parameter. Now it compiles, just don't ask

simou
  • 2,467
  • 4
  • 30
  • 39
0

You can do the following (I tested it out and it works fine for me with Scala 2.9.1 and Java 1.6):

 final Option<String> obj1 = Some.apply(null) // This will give you a None;
 System.out.println(obj1.getClass()); // will print "class scala.None$

 final Option<String> obj2 = Some.apply("Something");
 System.out.println(obj2.getClass()); //will print "class scala.Some
vladmore
  • 104
  • 3
  • I didn't see the answer @om-nom-nom posted when I wrote this but I think that should work as well. – vladmore Jan 30 '12 at 05:42
  • 1
    `Some(null)` is `Some(null)`, not `None`. – Daniel C. Sobral Jan 30 '12 at 12:42
  • That's what I expected, because it would make sense and that's what happens when I try that through the Scala interpreter. But when I try the code I posted above in a java file and run that I get obj2.getClass() giving me None$. I'm not sure why though. – vladmore Feb 01 '12 at 04:36
  • When I do this through Java it looks like because Some extends Option it calls Option's factory apply method which in turn returns None if the argument to apply is null. – vladmore Feb 01 '12 at 05:13
  • 3
    Oh, wow, that is something I had never imagined! Ok, here's the deal: Java has static inheritance, Scala doesn't. The object `Some` doesn't, and can't, extend the object `Option`, but Scala creates static forwarders from the class to the object unless some conflict shows up. The correct way to invoke `apply' is to do it on `scala.Some$.MODULE$` (or `Option$`, etc). It just happens that `Option` has static forwarders but `Some` doesn't, so _Java's_ static inheritance comes into play. So weird. – Daniel C. Sobral Feb 01 '12 at 14:08
  • Glad you were able to shed some light on this. I'm just getting started with Scala so I don't have knowledge of the language internals yet but I'm working on it. – vladmore Feb 01 '12 at 23:34