Reputation: 3390
abstract class Box {
type T <: String
}
type InvariantBox = Box { type T = AnyRef } // compiles
abstract class Box2 extends Box {
type T = AnyRef //Error:overriding type T in class
// Box with bounds <: String; type T
// has incompatible type type T = AnyRef
}
Why is it possible to redefine T
type parameter in InvariantBox but not allowed to do the same in Box2
?
Upvotes: 2
Views: 67
Reputation: 51658
Because you have the right to create intersection
type HasT = { type T = AnyRef }
type InvariantBox = Box with HasT
Otherwise not for any types A
, B
you could create type A with B
.
And for classes there are overriding rules.
Scala allows defining types without any values or with only null
value.
Upvotes: 1