This is not a valid type definition:
scala> type Addable = { def +(subject: Addable) }
<console>:4: error: illegal cyclic reference involving type Addable
type Addable = { def +(subject: Addable) }
Can this be expressed in scala?
Here's what I did in a library I wrote, HTH:
trait Addable {
type AddableType <: Addable
def + (subject: AddableType): AddableType
}
trait Rational extends Addable {
type AddableType = Rational
override def + (subject: Rational): Rational
}
No, it can't.
On page 40 of The Scala Language Specification Version 2.7:
However, it is a static error if a type alias refers recursively to the defined type constructor itself. That is, the type T in a type alias type t[tps] = T may not refer directly or indirectly to the name t.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With