KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am learning Scala and have a question regarding lower bound. I have a class: class Queue[+T] { def enqueue[U>:T](x : U) = new Queue[U]() } class Fruit class Apple extends Fruit class Orange extends Fruit class Another What I found is that, for a Queue of any type, e.g.: val q1 = new Queue[Fruit] All the three lines below will pass compile q1.enqueue(new Apple) q1.enqueue(new Orange) q1.enqueue(new Another) My question is: if we use lower bound to define U must be a super type of T, in the lines above, Apple is clearly not a super type of Fruit, how can it be passed to the enqueue function? The "Another" class is not in the fruit hierachy at all, how can it be used in the enqueue function? Please help me out with this. Regards Kevin
Tags (comma-separated)
Save Edits
Cancel