Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use scala.None from Java code [duplicate]

Possible Duplicate:
Accessing scala.None from Java

In Java you can create an instance of Some using the constructor, i.e. new Some(value), but None has no partner class. How do you pass None to a Scala function from Java?

like image 500
Craig B. Avatar asked Jan 04 '10 03:01

Craig B.


2 Answers

The scala.None$.MODULE$ thing doesn't always typecheck, for example this doesn't compile:

scala.Option<String> x = scala.None$.MODULE$; 

because javac doesn't know about Scala's declaration-site variance, so you get:

J.java:3: incompatible types found   : scala.None$ required: scala.Option<java.lang.String>     scala.Option<String> x = scala.None$.MODULE$ ; 

This does compile, though:

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

so that's a different way to get a None that is usable in more situations.

like image 113
Seth Tisue Avatar answered Sep 21 '22 00:09

Seth Tisue


I think this ugly bit will work: scala.None$.MODULE$

There is no need for a new instance since one None is as good as another...

like image 36
Mitch Blevins Avatar answered Sep 23 '22 00:09

Mitch Blevins