Bogdan Vakulenko
Bogdan Vakulenko

Reputation: 3390

Overriding abstract type members

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

Answers (1)

Dmytro Mitin
Dmytro Mitin

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

Related Questions