Scala converting recursively bounded type parameter (F-bounded) to type member

风格不统一 提交于 2019-12-03 08:24:16

问题


How would I convert:

trait Foo[A <: Foo[A]]

to a type member?

I.e., I want something along the lines of the following:

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

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?


回答1:


Use a self-type:

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


来源:https://stackoverflow.com/questions/14244260/scala-converting-recursively-bounded-type-parameter-f-bounded-to-type-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!