Skip to content

Instantly share code, notes, and snippets.

@notyy
Created January 1, 2012 16:39
Show Gist options
  • Save notyy/1547732 to your computer and use it in GitHub Desktop.
Save notyy/1547732 to your computer and use it in GitHub Desktop.
covariant
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