tags:

views:

223

answers:

3

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?

A: 

It seems this may be "fixed" in Scala 2.8:

http://lampsvn.epfl.ch/trac/scala/ticket/1291

skaffman
It does not work with the current nightly build.Welcome to Scala version 2.8.0.r18457-b20090810020144 (Java HotSpot(TM) Client VM, Java 1.6.0_12).Type in expressions to have them evaluated.Type :help for more information.scala> class A{ | type Addable = { def +(subject: Addable) } | }<console>:5: error: illegal cyclic reference involving method + type Addable = { def +(subject: Addable) }At least the error message changed :-).
Thomas Jung
Did you try the experimental command line option mentioned at the bottom of the above article?
skaffman
Same result with: scala -Yrecursion 10
Thomas Jung
+1  A: 

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 
  }
Yardena
The definition has to be a structural type as the underlining classes cannot be changed (like Int, Long).
Thomas Jung
Hm, perhaps you could define a view (implicit def) to convert these classes to Addable?
Yardena
+1  A: 
Walter Chang