Created
June 16, 2011 10:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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