Possible Duplicate:
Accessing scala.None from Java可能重复:从Java访问scala.None
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?
在Java中,您可以使用构造函数创建Some的实例,即new Some(value),但None没有合作伙伴类。如何从Java传递None到Scala函数?
5 个解决方案
#1
20
I think this ugly bit will work: scala.None$.MODULE$
我认为这个丑陋的部分会起作用:scala.None $ .MODULE $
There is no need for a new instance since one None is as good as another...
不需要新实例,因为一个None和另一个一样好......
#2
51
The scala.None$.MODULE$
thing doesn't always typecheck, for example this doesn't compile:
scala.None $ .MODULE $ thing并不总是类型检查,例如这不能编译:
scala.Option<String> x = scala.None$.MODULE$;
because javac doesn't know about Scala's declaration-site variance, so you get:
因为javac不知道Scala的声明站点方差,所以你得到:
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.
这是获得在更多情况下可用的无法的另一种方式。
#3
11
You can access the singleton None instance from java using:
您可以使用以下命令从java访问singleton None实例:
scala.None$.MODULE$
#4
2
I've found this this generic function to be the most robust. You need to supply the type parameter, but the cast only appears once, which is nice. Some of the other solutions will not work in various scenarios, as your Java compiler may inform you.
我发现这个通用函数是最强大的。你需要提供type参数,但是只显示一次,这很好。其他一些解决方案不适用于各种场景,因为您的Java编译器可能会通知您。
import scala.None$;
import scala.Option;
public class ScalaLang {
public static <T> Option<T> none() {
return (Option<T>) None$.MODULE$;
}
}
public class ExampleUsage {
static {
//for example, with java.lang.Long
ScalaLang.<Long>none();
}
}
#5
1
Faced with this stinkfest, my usual modus operandi is:
面对这个臭味,我通常的做法是:
Scala:
object SomeScalaObject {
def it = this
}
Java:
doStuff(SomeScalaObject.it());
#1
20
I think this ugly bit will work: scala.None$.MODULE$
我认为这个丑陋的部分会起作用:scala.None $ .MODULE $
There is no need for a new instance since one None is as good as another...
不需要新实例,因为一个None和另一个一样好......
#2
51
The scala.None$.MODULE$
thing doesn't always typecheck, for example this doesn't compile:
scala.None $ .MODULE $ thing并不总是类型检查,例如这不能编译:
scala.Option<String> x = scala.None$.MODULE$;
because javac doesn't know about Scala's declaration-site variance, so you get:
因为javac不知道Scala的声明站点方差,所以你得到:
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.
这是获得在更多情况下可用的无法的另一种方式。
#3
11
You can access the singleton None instance from java using:
您可以使用以下命令从java访问singleton None实例:
scala.None$.MODULE$
#4
2
I've found this this generic function to be the most robust. You need to supply the type parameter, but the cast only appears once, which is nice. Some of the other solutions will not work in various scenarios, as your Java compiler may inform you.
我发现这个通用函数是最强大的。你需要提供type参数,但是只显示一次,这很好。其他一些解决方案不适用于各种场景,因为您的Java编译器可能会通知您。
import scala.None$;
import scala.Option;
public class ScalaLang {
public static <T> Option<T> none() {
return (Option<T>) None$.MODULE$;
}
}
public class ExampleUsage {
static {
//for example, with java.lang.Long
ScalaLang.<Long>none();
}
}
#5
1
Faced with this stinkfest, my usual modus operandi is:
面对这个臭味,我通常的做法是:
Scala:
object SomeScalaObject {
def it = this
}
Java:
doStuff(SomeScalaObject.it());