Skip to content

Instantly share code, notes, and snippets.

@kknd22
Last active December 23, 2015 10:39
Show Gist options
  • Save kknd22/6623310 to your computer and use it in GitHub Desktop.
Save kknd22/6623310 to your computer and use it in GitHub Desktop.
scala variance and bounds
1. List[+T] is convariant
2. def ::(elem child):List[me] ----> List[child] < List[me)
3. def ::[parent >: me](x: parent):List[parent] ----> parent::List[me] returns List[parent]
scala> class parent
defined class parent
scala> class me extends parent
defined class me
scala> class child extends me
defined class child
scala> val xs = List(new me)
xs: List[me] = List(me@2b64976a)
scala> val c= new child
c: child = child@71b653af
scala> val p = new parent
p: parent = parent@6e782d03
scala> c::xs
res45: List[me] = List(child@71b653af, me@2b64976a)
scala> p::xs
res46: List[parent] = List(parent@6e782d03, me@2b64976a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment