Scala将递归有界类型参数(F界)转换为类型成员 [英] Scala converting recursively bounded type parameter (F-bounded) to type member

查看:101
本文介绍了Scala将递归有界类型参数(F界)转换为类型成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何转换:

trait Foo[A <: Foo[A]]

成为类型成员?

即,我想要以下内容:

trait Foo {
  type A <: Foo {type A = ???}
}

但是我遇到了困难,因为名称A已经在类型优化中使用了.这个问题类似(并从中产生):通过类型成员而不是类型参数进行F界量化?

but I am having difficulty because the name A is already taken within the type refinement. This question is similar (and spawned from): F-bounded quantification through type member instead of type parameter?

推荐答案

使用自类型:

scala> trait Foo { self => type A <: Foo {type A = self.A}}
defined trait Foo

scala> class Bar extends Foo { type A = Bar }
defined class Bar

scala> class Bar extends Foo { type A = Int }
<console>:10: error: overriding type A in trait Foo with bounds <: Foo{type A = Bar.this.A};
 type A has incompatible type
       class Bar extends Foo { type A = Int }
                                    ^

这篇关于Scala将递归有界类型参数(F界)转换为类型成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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