0

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?

paulmdavies
  • 1,248
  • 3
  • 12
  • 28
  • The full signature actually is: `++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Seq[A], B, That]): That `:) You'll have to get at least a little bit familiar with `CanBuildFrom` magic when rolling out your own collections... – Patryk Ćwiek Feb 12 '14 at 17:12
  • Why is that not in the documentation? – paulmdavies Feb 12 '14 at 17:14
  • 2
    It is, you just have to open up the `++` method documentation and click `Full signature` at the bottom. AFAIR it was made that way not to scare people with the full signature when, from user perspective, it's not really important most of the time. Unless you're creating new collections, that is... – Patryk Ćwiek Feb 12 '14 at 17:17
  • 2
    Ugh. That's really confusing. I've created my own collections before but not tried to override these methods. Thanks. – paulmdavies Feb 12 '14 at 17:20

0 Answers0