从 Java 访问 scala.None

发布于 2024-12-29 17:33:03 字数 690 浏览 0 评论 0原文

如何从 Java 访问 scala.None?

最后一行导致编译器因“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>();

这会失败,并显示“无法找到符号构造函数 None()”:

final Option<String> object2 = new None();

这会失败,并显示“无法找到符号变量 None”:

final Option<String> object2 = None;

在 2007 年,这曾经可以工作,但后来 Scala 发生了变化。 Java 编译器给出错误:不兼容的类型

final Option<String> object2 = scala.None$.MODULE$;

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$;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

年华零落成诗 2025-01-05 17:33:03

这可能有效:

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

def apply [A] (x: A): 选项[A]
一个选项工厂,它创建
如果参数不为空,则为 Some(x);如果为空,则为 None。

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.

萌面超妹 2025-01-05 17:33:03

使用 Scala 2.10.2(不确定是什么版本出现的):

final Option<String> none = Option.empty();

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

final Option<String> none = Option.empty();
一刻暧昧 2025-01-05 17:33:03

不要过分强调这一点,但不应依赖静态转发器,因为它们在某些情况下可能不会生成。例如,Some 没有为其对象同伴提供静态转发器。

当前访问对象伴生方法的方法是这样的:

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

Option$Option 对象伴生的类。 MODULE$ 是一个最终的公共静态字段,其中包含 Option$ 的唯一实例(换句话说,对象同伴)。

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).

┼── 2025-01-05 17:33:03

您可以导入

import scala.compat.java8.OptionConverters;

然后:

OptionConverters.<YourType>toScala(Optional.empty())

You can import

import scala.compat.java8.OptionConverters;

And then:

OptionConverters.<YourType>toScala(Optional.empty())
自由如风 2025-01-05 17:33:03

您可以执行以下操作(我对其进行了测试,它在 Scala 2.9.1 和 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

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
久隐师 2025-01-05 17:33:03

使用 Scala 版本 2.11.4,在我的例子中唯一有效的是

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

Update: a day after...

Option.apply(null);

没有类型参数也可以工作,因为它应该。由于某种原因,我第一次尝试时无法让 Intellij 编译代码。我必须传递辅助类型参数。现在它可以编译了,只是不要问

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文