I study Scala and ask a question regarding the 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
I found that for a queue of any type, for example:
val q1 = new Queue[Fruit]
All three lines below will pass the compilation
q1.enqueue(new Apple) q1.enqueue(new Orange) q1.enqueue(new Another)
My question is: if we use the lower bound to determine that U should be a super-type of T, in the lines above, Apple is clearly not a supertype of Fruit, how can it be passed to the enqueue function?
The "other" class is not in the fruit hierarchy at all, how can it be used in the enqueue function?
Please help me with this.
Relations Kevin
scala
Kevin
source share