(T,T)*
解决Seq[(T,T)]
后擦除,但如何将(T,T)*
自己表示为一种类型?
我问的原因是我使用的 API 定义了:
def foo(bar: (String,String)*) = ...
但是当我尝试传入一个Seq[(String,String)]
.
我的拉取请求添加:
def foo(bar: Seq[(String,String)]) = ...
由于擦除后具有相同类型的两种方法而爆炸。
星形投影可以表示为具体类型吗?
(T,T)*
解决Seq[(T,T)]
后擦除,但如何将(T,T)*
自己表示为一种类型?
我问的原因是我使用的 API 定义了:
def foo(bar: (String,String)*) = ...
但是当我尝试传入一个Seq[(String,String)]
.
我的拉取请求添加:
def foo(bar: Seq[(String,String)]) = ...
由于擦除后具有相同类型的两种方法而爆炸。
星形投影可以表示为具体类型吗?
Seq
如果你遵循它,你可以像这样传递:_*
:
val s:Seq[(String, String)] = Seq( ("a", "b"), ("c", "d"), ... )
foo(s:_*)
所以你不应该需要两个签名。
要消除已擦除签名的歧义:
scala> class X { def f(is: Int*) = is.sum }
defined class X
scala> class Y extends X { def f(is: Seq[Int])(implicit d: DummyImplicit): Int = f(is: _*) }
defined class Y
scala> new Y().f(1 to 10)
res3: Int = 55
或者这样更好,集合中的签名总是看起来像这样表示“两个或更多”:
scala> class X {
| def f(i: Int): Int = i
| def f(is: Seq[Int]): Int = is.sum
| def f(i: Int, j: Int, rest: Int *): Int = i + j + rest.sum
| }
defined class X
scala> new X().f(3)
res9: Int = 3
scala> new X().f(3,4)
res10: Int = 7
scala> new X().f(3,4,5)
res11: Int = 12
scala> new X().f(1 to 10)
res12: Int = 55
您不能引用重复的参数类型,就像您不能引用按名称的参数类型一样。所以你不能转换成它。但是,您可以通过名称反射性地检测到它:
scala> import reflect.runtime.universe._
import reflect.runtime.universe._
scala> typeOf[X].member(TermName("f")).asMethod.paramss.flatten.head.asTerm.typeSignature.typeSymbol.name
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
res4: reflect.runtime.universe.Symbol#NameType = <repeated>
有内部 API,definitions.isRepeated(sym)
如果你想为它转换。