scala - Referring abstract type member of a type parameter -
my scenario like:
trait { type b def foo(b: b) } trait c[d <: a] { val d: d def createb(): d#b def bar() { d.foo(createb) } }
in repl, complains
<console>:24: error: type mismatch; found : d#b required: c.this.d.b a.bar(createb())
what's wrong ? , (if possible @ all) how correct code ?
d#b
type projection, , not same d.b
. have type mismatch because in foo
, b
actually meant this.b
, said not same d#b
(the latter being more general). informally, can think of d#b
as representing possible type abstract type b
can take instance of d
, while d.b
type of b
specific instance d
.
see what `#` operator mean in scala? , what meant scala's path-dependent types? context.
one way make compile changing createb
's return type d.b
:
def createb(): d.b
however in many cases such solution restrictive because tied specific instance d
, might not had in mind. solution replace abstract type type parameter (though more verbose):
trait a[b] { def foo(b: b) } trait c[b, d <: a[b]] { val d: d def createb(): b def bar() { d.foo(createb) } }
Comments
Post a Comment