I came across some strange situation in Scala today when I tried to clarify type restrictions in an abstract type member.
I have two traits that define the boundaries of an element of a type and combine them in a particular class. This works fine, but when matching / casting with a combination of tags, only one of the two TypeBounds is “active”, and I cannot understand why ...
I tried to prepare an example:
trait L
trait R
trait Left {
type T <: L
def get: T
}
trait Right {
type T <: R
}
now if i combine these two features in one particular class
val concrete = new Left with Right {
override type T = L with R
override def get: T = new L with R {}
}
I can access my member via get as intended
val x1: L with R = concrete.get
( ) , . , , .
// can only access as R, L with R won't work
val x2: R = concrete.asInstanceOf[Left with Right].get
// can only access as L, L with R won' work
val x3: L = concrete.asInstanceOf[Right with Left].get
, Left with Right - , Right Left, , ?
- , ?