4 ms·
> "why can't I add a String to a List[_ <: String]" is a common newcomer question Can you explain this, or point to a source that does?
by tomp 12y ago
> "why can't I add a String to a List[_ <: String]" is a common newcomer question
Can you explain this, or point to a source that does?
- lmm 12y agoString was a poor example, let's make it "why can't I add a Long to a List[_ <: Number]". With the more verbose syntax "List[T] forSome {type T <: Number}" the issue becomes much more obvious IMO - T could e.g. be Float.
- tomp 12y agoAha, that makes more sense. I interpret a type like `List[_ <: Number]` as a list with any kind of elements, whereas `List[T] forSome {type T <: Number}` makes sense to be a list with a specific kind of elements, with the type of the elements constrained (of course, assuming that List is invariant).
- platz 12y agoforsome vs forall