2

Scala List is declared as

sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with     Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]] with java.io.Serializable

The method to prepend an element to a List is declared as

def +:(elem: A): List[A]

As type A is covariant, why the compiler does not complain since A appears in the contravariant position in +:?

2
  • could you reiterate your question, I'm not sure what you're asking Dec 12, 2015 at 10:26
  • 1
    I was wondering why the signature of method +: of class List does not break the rule of usage of covariant.
    – Chen
    Dec 13, 2015 at 4:00

1 Answer 1

3

Because its full signature is:

def +:[B >: A, That](elem: B)(implicit bf: CanBuildFrom[List[A], B, That]): That

The doc what you mentioned in the question is just the simplified one, you need to check the full signature of the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.