开发者

Scala type bounds for varargs methods

I have the following base class:

trait Foo
abstract class Bar[A <: Foo](f : A*) extends Foo

I want to then create a subclass:

case class Baz(f : Foo*) extends Bar(f)

However, when I do this the compiler gets upset, telling me that:

inferred type arguments [Seq[Foo]] do not conform to class Bar's type parameter bounds [A <: Foo]

Adding an explicit type parameter to Bar, as in

case class Baz(f : Foo*) extends Bar[Foo开发者_开发百科](f)

then complains of a type mismatch - found Foo* when required Foo.

What's causing this? How should I get around it?


Written as above, the compiler expects a sequence of coma-delimited instances of type Foo as arguments to ctor of Bar, while it is being given a single instance of type Foo*, which is translated to a Seq[Foo] behind the scene.

Do this:

case class Baz(f : Foo*) extends Bar[Foo](f: _*)

This is how repeated parameters are passed around between all the methods, not just ctors. The below will then work as well:

case class Baz(f : Foo*) extends Bar(f: _*)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜