如何定义循环类型定义?

时间:2009-08-10 07:19:36

标签: scala

这不是有效的类型定义:

scala>  type Addable = { def +(subject: Addable) }
<console>:4: error: illegal cyclic reference involving type Addable
        type Addable = { def +(subject: Addable) }

这可以用scala表示吗?

3 个答案:

答案 0 :(得分:4)

不,它不能。

Scala语言规范版本2.7的第40页:

  

但是,如果a是静态错误   type alias以递归方式引用   定义的类型构造函数本身。那   是,类型别名中的类型 T    类型 t [tps] = T 可能无法直接或间接引用该名称   

答案 1 :(得分:1)

这就是我在我写的库中所做的,HTH:

  trait Addable {
    type AddableType <: Addable
    def + (subject: AddableType): AddableType
  }
  trait Rational extends Addable {
    type AddableType = Rational
    override def + (subject: Rational): Rational 
  }

答案 2 :(得分:0)

似乎这可能会在Scala 2.8中“修复”:

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

相关问题