Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 448
Alex DiCarlo Avatar asked Jan 09 '13 18:01

Alex DiCarlo


1 Answers

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 }
                                    ^
like image 163
kiritsuku Avatar answered Sep 22 '22 05:09

kiritsuku