Hi,
When I'm writing this code, I've got a Compile error in Scala
var s: Stack[_ <: Number] = new Stack[Integer];
s.push(new Integer(1)); //compile-error: type mismatch; found :Integer required: _$bqjyh where type _$bqjyh <: Number
s.push(null); //compile-error: type mismatch; found : Null(null) required: _$2 where type _$2 <: Objects.Vehicle
This is equivalent to covariant collection in Java due the wildcard; it exact type in unknown, so we cannot added something to the stack.
But with lists I won't get the same error:
var list: List[_ <: Number] = Nil;
var intList : List[Integer] = new Integer(1) :: Nil;
list = intList ; //no error
list = new Float(2) :: vehicles; //even float allowed
Now I can added even a float
, but in fact I would believe the list
is a List
of Integers
, so not Floats
allowed.
1) Why is this allowed with lists, and not with Stacks? Is this due to the cons (::) operator?
2) What is the type of list? Is it dynamic?
3) Why is this allowed in Scala and not in Java?
4) Can I add something to the stack? (null
is not working, in Java does because generic types only allows reference types)