如何在 Java 代码中使用 scala.None

发布于 2024-08-16 16:27:23 字数 330 浏览 4 评论 0原文

可能的重复:
从 Java 访问 scala.None

在 Java 中,您可以创建 Some 的实例 使用构造函数,即 new Some(value),但 None 没有伙伴类。如何从 Java 将 None 传递给 Scala 函数?

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?

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

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

发布评论

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

评论(5

场罚期间 2024-08-23 16:27:23

scala.None$.MODULE$ 东西并不总是进行类型检查,例如这不会编译:

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

因为 javac 不知道 Scala 的声明站点差异,所以你得到:

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

这确实可以编译,不过:

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

所以这是获得可在更多情况下使用的 None 的不同方法。

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.

智商已欠费 2024-08-23 16:27:23

我认为这个丑陋的部分会起作用: scala.None$.MODULE$

不需要新实例,因为一个 None 与另一个 None 一样好......

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

我ぃ本無心為│何有愛 2024-08-23 16:27:23

您可以使用以下方法从 java 访问单例 None 实例:

scala.None$.MODULE$

You can access the singleton None instance from java using:

scala.None$.MODULE$
冬天旳寂寞 2024-08-23 16:27:23

我发现这个通用函数是最强大的。您需要提供类型参数,但强制转换只出现一次,这很好。其他一些解决方案在各种情况下都不起作用,您的 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();
    }
}

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.

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();
    }
}
人事已非 2024-08-23 16:27:23

面对这种臭味,我惯用的做法是:

Scala:

object SomeScalaObject {
  def it = this
}

Java:

doStuff(SomeScalaObject.it());

Faced with this stinkfest, my usual modus operandi is:

Scala:

object SomeScalaObject {
  def it = this
}

Java:

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