Categories
Tech

Covariant, Contravariant, Contrabass, Codependent

I can never remember Scala‘s notation to indicate a covariant type vs. a contravariant type, so here it is:

Covariant [+A]

List can contain an object of type A or any subtype of A:

class List[+A]

Contravariant [-A]

Function1 accepts as an input an object of type T1 or any supertype of T1 (its return type R, however, is covariant):

trait Function1[-T1, +R]

Upper Type Bound [T <: A]

The following findSimilar function (borrowed from this example) accepts an instance of and a List of Similar objects or of objects that are subtypes of Similar:

def findSimilar[T <: Similar](e: T, xs: List[T]): Boolean

Lower Type Bound [T >: A]

Option#orElse accepts (and returns) an Option containing an object of type A or any supertype of A:

final def orElse[B >: A](alternative: ⇒ Option[B]): Option[B]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.