如何定义循环类型定义? [英] How to define a cyclic type definition?

查看:40
本文介绍了如何定义循环类型定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是有效的类型定义:

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) }

这可以用scala表达吗?

Can this be expressed in scala?

推荐答案

不,不能.

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

On page 40 of The Scala Language Specification Version 2.7:

然而,如果一个类型别名递归地指向定义类型构造函数本身.那即,类型别名中的类型 Ttype t[tps] = T 不得直接或间接提及名称t.

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.

这篇关于如何定义循环类型定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆