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

Comments

Popular posts from this blog

I get wrong characters when retreiving the message body of an email using TIdIMAP4.UIDRetrieveTextPeek2()

How to drop the all the 1's in a correlation matrix

Today Walkin 14th-Sept