不推荐通过插入 () 来调整参数列表 [英] Adaptation of argument list by inserting () has been deprecated

查看:32
本文介绍了不推荐通过插入 () 来调整参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从 Scala 2.10.x 升级到 2.11.2,我收到以下警告,代码如下:

 override def validateKey(key: String): 要么[InvalidKeyError, Unit] =键.包含(键)匹配{情况为真 =>正确的()case false =>左(无效键错误(上下文,键))}

<块引用>

不推荐通过插入 () 来调整参数列表:this不太可能是你想要的.签名: Right.apply[A, B](b: B):scala.util.Right[A,B] 给定参数:改编后:Right((): 单位)

我可以通过将true"case语句更改为:

case true =>Right(())//() 是 Unit 实例的快捷方式

这是解决此警告的正确方法吗?

编辑:也许为什么我们现在必须这样做"类型的答案是合适的,我的粗略调查似乎表明 Scala 在认为需要导致单元"时插入单元"其他问题

解决方案

Automatic Unit inference 已在 scala 2.11 中被弃用,其背后的原因是它会导致令人困惑的行为,尤其是对于学习该语言的人而言.

这是一个例子

class Foo[T](value: T)val x = 新 Foo

这不应该编译,对吧?您正在调用没有参数的构造函数,其中需要一个.令人惊讶的是,在 Scala 2.10.4 之前,它编译得很好,没有错误或警告.

那是因为编译器推断了一个 Unit 参数,所以它实际上用

替换了你的代码

val x = new Foo[Unit](())//Foo[Unit]

正如新引入的警告消息所说,这不太可能是您想要的.

另一个著名的例子是这个

scala>列表(1,2,3).toSet()//res1: Boolean = false

调用 toSet() 应该是一个编译时错误,因为 toSet 不接受参数,但编译器拼命试图让它编译,最终解释代码作为

scala>列表(1,2,3).toSet.apply(())

表示:测试()是否属于集合.由于情况并非如此,您会得到 false!

因此,从 scala 2.11 开始,如果您想将 ()(又名 Unit)作为参数传递,则必须明确.这就是为什么你必须写:

Right(())

代替

Right()

<小时>

示例取自 简化 Scala — 过去、现在和未来西蒙·奥克森莱瑟.

I'm just in the process of upgrading from Scala 2.10.x to 2.11.2 and I'm receiving the following warning with the following code:

  override def validateKey(key: String): Either[InvalidKeyError, Unit] = 
    keys.contains(key) match {
      case true => Right()
      case false => Left(InvalidKeyError(context, key))
    }

Adaptation of argument list by inserting () has been deprecated: this is unlikely to be what you want. signature: Right.apply[A, B](b: B): scala.util.Right[A,B] given arguments: after adaptation: Right((): Unit)

I am able to solve this by changing the "true" case statement to:

case true => Right(()) //() is a shortcut to a Unit instance

Is this the proper way to address this warning?

Edit: perhaps a "why we have to do this now" type answer would be appropriate, my cursory investigation seems to indicate that Scala inserting "Unit" when it thinks it needs to causes other problems

解决方案

Automatic Unit inference has been deprecated in scala 2.11, and the reason behind this is that it can lead to confusing behavior, especially for people learning the language.

Here's an example

class Foo[T](value: T)
val x = new Foo

This should not compile, right? You are calling the constructor with no arguments, where one is required. Surprisingly, until scala 2.10.4 this compiles just fine, with no errors or warnings.

And that's because the compiler inferred a Unit argument, so it actually replaced your code with

val x = new Foo[Unit](()) // Foo[Unit]

As the newly introduced warning message says, this is unlikely to be what you want.

Another famous example is this

scala> List(1,2,3).toSet()
// res1: Boolean = false

calling toSet() should be a compile-time error, since toSet does not take arguments, but the compiler desperately tries to make it compile, ultimately interpreting the code as

scala> List(1,2,3).toSet.apply(())

which means: test whether () belongs to the set. Since it's not the case, you get a false!

So, starting from scala 2.11, you have to be explicit if you want to pass () (aka Unit) as an argument. That's why you have to write:

Right(())

instead of

Right()


examples taken from Simplifying Scala — The Past, Present and Future by Simon Ochsenreither.

这篇关于不推荐通过插入 () 来调整参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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