无法在 Scala 中创建包含 null 的元组

发布于 2024-12-11 01:25:16 字数 596 浏览 0 评论 0原文

以下代码无法编译:

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 技术交流群。

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

发布评论

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

评论(5

森罗 2024-12-18 01:25:16

可能是因为您的 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.

糖果控 2024-12-18 01:25:16

我不确定您尝试使用 null 归档什么(运行时更多 NPE 除外),但我建议您不要在 Scala 代码中使用 null 。 Scala 有 Option 类型,可以用来代替 null。因此,在您的情况下,它看起来像这样:

var next: (Option[A], A) = (None, n)

我发现这段代码更清晰,它的一大优点是元组类型告诉其他人(和编译器)它的第一个元素不是某个 A,而是它可以是 Some[A]None

如果您需要与使用 null 的现有 Java 代码集成,您可以尽快将来自 Java 端的对象包装在 option 中。你可以这样写:

var next: (Option[A], A) = (Option(getAFromSomeMethodThatCanReturnNull()), n)

这个答案可能没有回答你原来的问题,但我希望它能有所帮助。

I'm not sure what you are trying to archive by using nulls (except more NPE at runtime), but I can recommend you not to use null in your Scala code. Scala has Option type that can be used instead of null. So in your case it would look like this:

var next: (Option[A], A) = (None, n)

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 either Some[A] or None.

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:

var next: (Option[A], A) = (Option(getAFromSomeMethodThatCanReturnNull()), n)

This answer probably does not answer your original question, but I hope it would be somehow helpful.

凹づ凸ル 2024-12-18 01:25:16

迪迪尔德是对的。如果您能够确保 A <: AnyRefA >: Null 它将起作用。

scala> def createTuple[A >: Null](first: A, second: A) = (first, second)
createTuple: [A >: Null](first: A, second: A)(A, A)

scala> val next = creatTuple(null, "n")
next: (java.lang.String, java.lang.String) = (null,n)

didierd is right. If you are able to ensure A <: AnyRef or A >: Null it will work.

scala> def createTuple[A >: Null](first: A, second: A) = (first, second)
createTuple: [A >: Null](first: A, second: A)(A, A)

scala> val next = creatTuple(null, "n")
next: (java.lang.String, java.lang.String) = (null,n)
屋檐 2024-12-18 01:25:16

对于 REPL 中的快速解决方案,您可以省略类型声明:

scala> var next = ((null: A), n)
next: (A, A) = (null,A@a9de5c)

scala> def foo (pa : (A, A)) { println ("unit") }
foo: (pa: (A, A))Unit

scala> foo (next) 
unit

但是,我建议也使用选项。

For a fast solution in the REPL, you might omit the type declaration:

scala> var next = ((null: A), n)
next: (A, A) = (null,A@a9de5c)

scala> def foo (pa : (A, A)) { println ("unit") }
foo: (pa: (A, A))Unit

scala> foo (next) 
unit

However, I would suggest using an Option too.

一生独一 2024-12-18 01:25:16

我刚刚遇到这个问题;在泛型类型 [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.

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