Search code examples
scalagenericscovariance

scala type system - understanding covariant with lower/upper bounds


I am new to scala and have questions on scala generics

enter image description here

Question is how animalContainer.add method is accepting new Cat. From my understanding B>:A - A is Animal and B is Cat. Cat is NOT super typs of animal . How it is working..


Solution

  • B is not Cat, B is Animal. The inferred type of add is then effectively:

    def add(element: Animal)
    

    You are able to pass Cat here because Cat extends Animal.