Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Contravariance is the Dual of Covariance - Erik...

Contravariance is the Dual of Covariance - Erik Meijer

Joy of Coding

March 07, 2014
Tweet

More Decks by Joy of Coding

Other Decks in Technology

Transcript

  1. Side Effects val steveb: ()=>String
 steveb() // “developer” steveb() //

    “blah” steveb() // “Windows 8” steveb() // ☲
  2. Side Effects val sjobs: ()=>String
 sjobs() // “iPhone” sjobs() //

    “iPad” sjobs() // “iCloud” sjobs() // †
  3. Interfaces trait Enumerable[+T] {
 def getEnumerator(): Enumerator[T]
 } ! trait

    Enumerator[+T] {
 def moveNext(): Boolean
 def current: T
 }
  4. Lifting trait Enumerable[+T] {
 def getEnumerator(): Enumerator[T] ! def lift(f:

    Enumerator[T]=>Enumerator[S]):
 val that = this Enumerable[S] = {
 new Enumerable[S] {
 def getEnumerator() = f(that.GetEnumerator()) }
 }
 }
  5. Interfaces trait Observable[+T] {
 def Subscribe(o: Observer[T]): Unit
 } !

    trait Observer[-T] {
 def onCompleted(): Unit
 def onError(error: Throwable): Unit
 def onNext(value: T): Unit
 }
  6. Lifting trait Observable[+T] {
 def subscribe(o: Observer[T]) ! def lift(f:

    Observer[S]=>Observer[T]):
 val that = this Observable[S] = {
 new Observable[S] {
 def subscribe(o: Observer[S]) = that.Subscribe(f(o)) }
 }
 }