我突然遇到了这种(出乎我意料的)情况:
def method[T](x: T): T = x
scala> method(1)
res4: Int = 1
scala> method(1, 2)
res5: (Int, Int) = (1,2)
为什么在两个或更多参数的情况下方法返回并推断出一个元组但抛出有关参数列表的错误?
是故意的吗?也许这种现象有名字?
最佳答案
% scala2.10 -Xlint
scala> def method[T](x: T): T = x
method: [T](x: T)T
scala> method(1)
res1: Int = 1
scala> method(1, 2)
<console>:9: warning: Adapting argument list by creating a 2-tuple: this may not be what you want.
signature: method[T](x: T): T
given arguments: 1, 2
after adaptation: method((1, 2): (Int, Int))
method(1, 2)
^
res2: (Int, Int) = (1,2)
关于Scala 类型参数被推断为元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12806982/