Created
January 1, 2012 16:39
-
-
Save notyy/1547732 to your computer and use it in GitHub Desktop.
covariant
This file contains hidden or 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
class Father { | |
def say() { | |
Console.println("I am father") | |
} | |
} | |
class Son extends Father { | |
override def say() { | |
Console.println("I am son") | |
} | |
} | |
def speak(speakers:List[Father]) = speakers map (_.say) | |
val sFathers = List(new Father) | |
val sSons = List(new Son) | |
import java.util.ArrayList | |
def javaSpeak(speakers:ArrayList[Father]) = speakers.get(0).say | |
val jFathers = new java.util.ArrayList[Father]() | |
jFathers.add(new Father) | |
val jSons = new java.util.ArrayList[Son]() | |
jSons.add(new Son) | |
---------------------------------------------- | |
speak(sFathers) //OK | |
speak(sSons) //OK | |
javaSpeak(jFathers) //OK | |
javaSpeak(jSons) //wrong! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment