Skip to content

Instantly share code, notes, and snippets.

@kings13y
Created June 16, 2011 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kings13y/1028978 to your computer and use it in GitHub Desktop.
Save kings13y/1028978 to your computer and use it in GitHub Desktop.
cut-and-paste from the awesome Variance annotations example from: http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInheritance
// Taken from the excellent tutorial here:
// http://programming-scala.labs.oreilly.com/ch12.html#VarianceUnderInheritance
// Note this sample leverages the Function1 trait: Function1[-T, +R]
// WON'T COMPILE
class CSuper { def msuper = println("CSuper") }
class C extends CSuper { def m = println("C") }
class CSub extends C { def msub = println("CSub") }
def useF(f: C => C) = {
val c1 = new C // #1
val c2: C = f(c1) // #2
c2.msuper // #3
c2.m // #4
}
useF((c: C) => new C) // #5
useF((c: CSuper) => new CSub) // #6
useF((c: CSub) => {println(c.msub); new CSuper}) // #7: ERROR!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment