从Java访问scala.None [英] Accessing scala.None from Java

查看:395
本文介绍了从Java访问scala.None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Java访问 scala.None

最后一行导致编译器死亡键入scala.None不接受参数。

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>();

这失败,找不到符号构造函数None():

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;

在2007年,这曾经有效,但随后Scala发生了变化。 Java编译器提供错误:不兼容的类型

In 2007 this used to work, but then Scala changed. The Java compiler gives error: incompatible types:

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


推荐答案

这可能有效:

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




def apply [A](x:A):选项[A ]

创建
的Option工厂如果参数不为null,则为(x),如果为null,则为None。

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.

这篇关于从Java访问scala.None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆