Section 4.6.2 of the Scala Language Specification Version 2.8 describes repeated parameters and says:
The last value parameter of a parameter section may be suffixed by “*”, e.g. (..., x:T*). The type of such a repeated parameter inside the method is then the sequence type scala.Seq[T].
However, this code:
abstract class A { def aSeq : Seq[A] }
class B(val aSeq : A*) extends A
class C extends B { override val aSeq :Seq[A] = Seq() }
give an error when compiled:
overriding value aSeq in class B of type A*; value aSeq has incompatible type
The compiler seems to indicate that A* is a distinct type from Seq[A].
Investigating the actual class of aSeq in this case shows it to be an instance of scala.collection.mutable.WrappedArray$ofRef but even the following code fails to compile with the same message:
class C extends B { override val aSeq = new ofRef(Array[A]()) }
So the question is, how do I go about overriding a member defined by a repeated parameter on the class?
In case you're wondering where this is coming from, that is exacly what scala.xml.Elem does to override the child method in scala.xml.Node.
vals in a class (I am a bit surprised it compiles at all). Instead haveclass B(val aSeq: Seq[A]) extends Aand if you want to have syntaxB(a1, a2, ...)available, add it in the companion object:object B {def apply(aSeq : A*) = new B(aSeq)}. - Alexey Romanovscala.xml.Nodeand notscala.xml.Elem. - Alexey Romanovoverride val seq: Int* = Seq(1,2)andoverride val seq = Seq(1,2): _*. Unfortunately, neither works. I suspect it may be impossible, but hopefully this is wrong. - Alexey Romanov