简体   繁体   中英

scala type system - understanding covariant with lower/upper bounds

I am new to scala and have questions on scala generics

在此处输入图片说明

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM