Frage

(T,T)* resolves to Seq[(T,T)] after erasure, but how to represent (T,T)* itself as a type?

The reason I ask is there's an API I'm using that defines a:

def foo(bar: (String,String)*) = ...

but fails when I try to pass in a Seq[(String,String)].

My pull request to add in:

def foo(bar: Seq[(String,String)]) = ...

blows up due to the 2 methods having the same type after erasure.

Are star projections able to be represented as a concrete type?

War es hilfreich?

Lösung

You can pass the Seq if you follow it with :_* like this:

val s:Seq[(String, String)] = Seq( ("a", "b"), ("c", "d"), ... )
foo(s:_*)

So you shouldn't need both signatures.

Andere Tipps

To disambiguate the erased signatures:

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

or this is better, the signatures in collections always look like this to mean "two or more":

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

You can't refer to a repeated parameter type, just as you can't refer to a by-name parameter type as such. So you can't convert to it. However, you can detect it reflectively, by name:

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>

There is internal API, definitions.isRepeated(sym), if you want to cast for it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top