Type bounds and variance annotations cover unrelated issues.
- A type bound specifies constraints on allowed types that can be used for a type parameter in a parameterized type. For example, T <: AnyRef limits T to be a subtype of AnyRef.
- A variance annotation specifies when an instance of a subtype of a parameterized type can be substituted where a supertype instance is expected. For example, because List[+A] is covariant in T, List[String] is subtype of List[Any].
All the parameterized types are immutable types.
For example,
class ContainerPlus[+A](val value: A)
The problem with a mutable field is that it behaves like a private field with public read and write accessor methods.
No comments:
Post a Comment