无法在 Scala 中创建包含 null 的元组
以下代码无法编译:
var next: (A, A) = (null, n)
Error:
error: type mismatch;
found : Null(null)
required: A
var next: (A, A) = (null, n)
Also var next: (A, A) = ((null: A), n)
失败并出现相同的错误。
我认为它应该编译。
我当前正在使用以下代码,它似乎可以工作:
var next: (A, A) = (null.asInstanceOf[A], n)
为什么它不起作用?错误还是功能?
编辑
阅读 didier 的回答后,问题很明显。我错过了 null
只能分配给 AnyRef
类型。对于我的问题,我选择创建元组 (n,n)
并有一个布尔标志第一个条目是否有效。根据应用程序Option
可能是更好的解决方案。
The following code does not compile:
var next: (A, A) = (null, n)
Error:
error: type mismatch;
found : Null(null)
required: A
var next: (A, A) = (null, n)
Also var next: (A, A) = ((null: A), n)
fails with the same error.
Somehow I think it should compile.
I'm currently using the following code which seems to work:
var next: (A, A) = (null.asInstanceOf[A], n)
Why doesn't it work? Bug or feature?
Edit
After reading didiers answer the problem is obvious. I missed that null
can only be assigned to AnyRef
types. For my problem I choose to make the tuple (n,n)
and have a boolean flag whether the first entry is valid. Depending on the application Option
might be a better solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可能是因为您的 A 参数类型不能保证为 <: AnyRef。如果您打算传递 null,则应该如此。如果是这样,我会说功能
编辑。请参阅下面丹尼尔的评论,解决方案是>:Null,也请参阅AgileSteel答案。
Probably because your A parameter type is not guaranteed to be <: AnyRef. It should if you intend to pass null. If so, I would say feature
Edit. See Daniel's comment below, the solution is >: Null, See AgileSteel answer too.
我不确定您尝试使用
null
归档什么(运行时更多 NPE 除外),但我建议您不要在 Scala 代码中使用null
。 Scala 有Option
类型,可以用来代替null
。因此,在您的情况下,它看起来像这样:我发现这段代码更清晰,它的一大优点是元组类型告诉其他人(和编译器)它的第一个元素不是某个
A
,而是它可以是Some[A]
或None
。如果您需要与使用
null
的现有 Java 代码集成,您可以尽快将来自 Java 端的对象包装在 option 中。你可以这样写:这个答案可能没有回答你原来的问题,但我希望它能有所帮助。
I'm not sure what you are trying to archive by using
null
s (except more NPE at runtime), but I can recommend you not to usenull
in your Scala code. Scala hasOption
type that can be used instead ofnull
. So in your case it would look like this:I find this code much more clear and it's big advantage, is that tuple type tells other people (and compiler) that it's first element is not some
A
, but it eitherSome[A]
orNone
.If you need to integrate with existing Java code that uses
null
, that you can wrap objects that come from Java side in option as soon as possible. You can make it like this:This answer probably does not answer your original question, but I hope it would be somehow helpful.
迪迪尔德是对的。如果您能够确保
A <: AnyRef
或A >: Null
它将起作用。didierd is right. If you are able to ensure
A <: AnyRef
orA >: Null
it will work.对于 REPL 中的快速解决方案,您可以省略类型声明:
但是,我建议也使用选项。
For a fast solution in the REPL, you might omit the type declaration:
However, I would suggest using an Option too.
我刚刚遇到这个问题;在泛型类型
[A]
的函数中,我需要使用(List[A], A)
类型的元组。以下构造工程:
(Nil, null.asInstanceOf[A])
。适用于 Scala 2.10 和 2.11。
I just encountered this problem; in function with generic type
[A]
, I need to use a tuple of type(List[A], A)
.Following construction works:
(Nil, null.asInstanceOf[A])
.Works for Scala 2.10 and 2.11.