关于Scala中(递归)结构类型的有趣观察

我在一些代码中需要一些递归结构类型,使用特征和结构类型作为类型参数约束。它工作正常,但后来我了解到Scala不支持递归结构类型。 那么有人可以解释为什么这样可行:
scala> trait Test[M[A] <: { def map[B](f: A => B) : M[B] } ] {}
defined trait Test
而这不是:
scala> def test[M[A] <: { def map[B](f: A => B) : M[B] } ] = null
<console>:5: error: illegal cyclic reference involving type M
       def test[M[A] <: { def map[B](f: A => B) : M[B] } ] = null
    
已邀请:
我认为这是编译器中的一个小故障。以下代码表现出与初始代码相同的行为:
trait Test[M[A] <: { def map: M[A] } ] {}
def test[M[A] <: { def map: M[A] } ] = null
它导致编译时错误:'非法循环引用'。 并且以下代码没有(即它编译好):
type S[M] = { def map: M }

trait Test[M[A] <: S[M[A]] ] {}
def test[M[A] <: S[M[A]] ] = null
唯一的区别是结构类型是通过类型别名S在这里应用的。     
第一个代码片段也在Scala 2.7.7final中抛出错误:
scala> trait Test[M[A] <: { def map[B](f: A => B) : M[B] } ] {}
<console>:4: error: illegal cyclic reference involving type M
       trait Test[M[A] <: { def map[B](f: A => B) : M[B] } ] {}
                                                    ^
您使用的是哪个版本的Scala?     

要回复问题请先登录注册