Skip to content

Instantly share code, notes, and snippets.

@shivangi1015
Created May 3, 2018 06:01
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 shivangi1015/dd50cfa4d10696d647f39c3ef31791b7 to your computer and use it in GitHub Desktop.
Save shivangi1015/dd50cfa4d10696d647f39c3ef31791b7 to your computer and use it in GitHub Desktop.
scala> class Animal
defined class Animal
scala> class Dog extends Animal
defined class Dog
scala>import java.util.ArrayList
scala> def printAnimals(animal: ArrayList[Animal]) = {
| for(i <- 0 to animal.size - 1) {
| println(animal.get(i))
| }
| }
printAnimals: (animal: java.util.ArrayList[Animal])Unit
scala> var dogsList = new ArrayList[Dog]
dogsList: java.util.ArrayList[Dog] = []
scala> printAnimals(dogsList)
<console>:15: error: type mismatch;
found : java.util.ArrayList[Dog]
required: java.util.ArrayList[Animal]
Note: Dog <: Animal, but Java-defined class ArrayList is invariant in type E.
You may wish to investigate a wildcard type such as `_ <: Animal`. (SLS 3.2.10)
printAnimals(dogsList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment