In Scala 2.10, I'm trying to create my own SeqLike class which disallows duplicates. I'm attempting to do this by overriding the methods which add to it, calling distinct()
at the end of each of them:
sealed class UniqueSeq[A] private ( private val values : Seq[A] ) extends SeqLike[A,Seq[A]] with Seq[A]// with GenericTraversableTemplate[A,UniqueSeq]
{
def apply( idx : Int ) : A = values( idx )
def iterator = values.iterator
def length = values.length
override def ++[B]( that : GenTraversableOnce[B] ) : Seq[B] =
{
val res = values ++ that
res.distinct
}
}
However, the overridden ++ method doesn't compile. This is the signature as listed in the documentation for SeqLike, but res
is of type Seq[Any]
, and hence so is the return type.
I can't see how this method will ever work, since A
and B
are completely unrelated, I can only see the return type being Seq[Any]
- can someone shed some light on how I can override it correctly?