加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 由于广泛的递归类型参数,关于类图的编译器错误并不完备

发布时间:2020-12-16 09:19:20 所属栏目:安全 来源:网络整理
导读:用这段代码: trait B[T]trait C[T]class A[T] extends B[A[C[T]]] 我收到以下错误: error: class graph is not finitary because type parameter T is expansively recursive class A[T] extends B[A[C[T]]] ^ 有人可以解释错误信息是什么,为什么T是无限递
用这段代码:

trait B[T]
trait C[T]
class A[T] extends B[A[C[T]]]

我收到以下错误:

error: class graph is not finitary because type parameter T is expansively recursive
       class A[T] extends B[A[C[T]]]
               ^

有人可以解释错误信息是什么,为什么T是无限递归的,为什么下面的代码可以工作?

class A[T] extends B[A[T]]

解决方法

从 Scala 2.9 specification(注意,这是在变更日志中作为在2.4中引入的更改,所以它不是2.9的新的限制):

The implementation of subtyping has been changed to prevent infinite
recursions. Termination of subtyping is now ensured by a new
restriction of class graphs to be finitary.

Kennedy and Pierce解释为什么无限类图是一个问题:

Even disregarding subtyping,infinite closure presents a problem for
language implementers,as they must take care not to create type
representations for supertypes in an eager fashion,else non-
termination is the result. For example,the .NET Common Language
Runtime supports generic instantiation and generic inheritance in
its intermediate language targeted by C. The class loader maintains a
hash table of types currently loaded,and when loading a new type it
will attempt to load its supertypes,add these to the table,and in
turn load the type arguments involved in the supertype.

幸运的是,正如肯尼迪和皮尔斯所说,有一个方便的方法来检查一个班级图是不是无限的.我在这个答案中使用他们的定义.

首先,为了清楚起见,我将使您的类型变量不同:

trait B[X]
trait C[Y]
class A[Z] extends B[A[C[Z]]]

接下来,我们使用Kennedy和Pierce的定义构造类型参数依赖图.对于图形来说,添加边的唯一声明是对于A的最后一个声明.它们给出了构建图的以下规则:

For each declaration C <X?> <:: T and each subterm D<T?> of T,if T_j = X_i add a non-expansive edge C#i → D#j; if X_i is a proper subterm of T_j add an expansive edgeC#i → D#j

因此首先我们来看Z和C [Z],这给了我们从Z到Y的非扩展边.下一个Z和A [C [Z]]给出了从Z到Z的扩展边,Z和B [ A [C [Z]]]给出了从Z到X的扩展边:

我已经用虚线箭头表示非膨胀的边缘,并且具有固体边缘.我们有一个具有广阔边缘的周期,这是一个问题:

Infinitary class tables are characterized precisely by those graphs
that contain a cycle with at least one expansive edge.

对于类A [Z]扩展B [A [Z]],这不会发生,它具有以下图形:

如果是广泛的,请参阅论文以证明课桌是无限的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读