Scala 类型参数被推断为元组 [英] Scala type parameter being inferred to tuple
本文介绍了Scala 类型参数被推断为元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我突然遇到了这种(出乎我意料的)情况:
I suddenly came across this (unexpected to me) situation:
def method[T](x: T): T = x
scala> method(1)
res4: Int = 1
scala> method(1, 2)
res5: (Int, Int) = (1,2)
为什么在两个或更多参数的情况下,方法返回并推断出一个元组,但抛出有关参数列表的错误?是故意的吗?也许这种现象有名字?
Why in case of two and more parameters method returns and infers a tuple but throwing error about parameter list? Is it by intention? Maybe this phenomenon has a name?
推荐答案
% 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 类型参数被推断为元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文