Created
May 3, 2018 06:01
-
-
Save shivangi1015/dd50cfa4d10696d647f39c3ef31791b7 to your computer and use it in GitHub Desktop.
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
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