EECOLOR
EECOLOR

Reputation: 11244

Compiler allows implicit varargs but there seems no way to supply them implicitly

The following compiles:

def test(x:String)(implicit s:String *) = ???

However, there seems no way I can supply the implicit s.

def test2(x:String)(implicit s:String *) =
  test(x) // could not find implicit value for parameter s: String*

Am I missing something or should the compiler throw an exception?

Upvotes: 1

Views: 104

Answers (1)

0__
0__

Reputation: 67290

This is because inside the method body, the var-args parameters are seen as Seq[A] instead of the special type A*. You can explicitly expand the sequence again:

def test2(x:String)(implicit s:String*) = test(x)(s: _*)

Since you cannot define a type as String*, it renders the implicit case useless, though:

implicit val a = Seq("hallo", "gallo"): _*

// error: no `: _*' annotation allowed here

So you will need to use Seq[String].

Upvotes: 1

Related Questions