Skip to content

Instantly share code, notes, and snippets.

@ketankhairnar
Created November 17, 2010 09:55
Show Gist options
  • Save ketankhairnar/703208 to your computer and use it in GitHub Desktop.
Save ketankhairnar/703208 to your computer and use it in GitHub Desktop.
Scala Covariance and Contravariance
object ParameterizedType {
//class definitions
class Pet(val name: String) {
override def toString = name
}
class Dog(override val name: String) extends Pet(name)
//covariance example
def playWithPets[T <: Pet](pets: Array[T]) = {
print(pets.mkString(", "))
}
def main(args: Array[String]) {
val dogs = Array(new Dog("Rover"), new Dog("Goofy"))
playWithPets(dogs)
}
//contravariance example
def copyPets[S, D >: S](fromPets: Array[S], destPets: Array[D]) = {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment