2021-06-28

Extract element of given subclass from list of superclass

Given this code

class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
class Pitbull extends Dog{}

object MyClass {
    def main(args: Array[String]) {
        val animals: List[Animal] = List(new Dog(), new Cat(), new Pitbull(), new Dog(), new Cat())
        getElementOftype(animals, PitBull)
    }
    
    def getElementOftype(list: List[Animal], givenType: ???): Animal = {
        
    }
}

I want to extract the first element of type Pitbull from this List, How should I proceed with this?



from Recent Questions - Stack Overflow https://ift.tt/3vZpVeh
https://ift.tt/eA8V8J

No comments:

Post a Comment