scala - How can I implement concrete class which extends trait defining a method with type by the type parameter's type alias -
i ask advanced scala developers. problem access type alias belonging type parameters of class' parent.
case class mymodel(foo: string = "bar") case class mydispatcher() trait module[m, d] { type dispatcher = d type model = m } trait myspecificmodule[a <: module[_, _]] { def dispatcher(): a#dispatcher } class moduleclass extends module[mymodel, mydispatcher] { //... } class myspecificmoduleclass extends myspecificmodule[moduleclass] { override def dispatcher(): mydispatcher = mydispatcher() }
so myspecificmodule
extends generic trait, , should know type of dispatcher
method. in case of myspecificmoduleclass
should mydispatcher
. when try compile code getting compilation error because type of method, not same defined: a#dispatcher
, in reality is.
error:(21, 18) overriding method dispatcher in trait myspecificmodule of type ()_$2; method dispatcher has incompatible type override def dispatcher(): mydispatcher = mydispatcher() ^
i appreciate advice suggest. in advance, gabor
resolved
case class mymodel(foo: string = "bar") case class mydispatcher() trait abstractmodule { type dispatcher type model } trait module[m, d] extends abstractmodule { type dispatcher = d type model = m } trait myspecificmodule[a <: abstractmodule] { def dispatcher(): a#dispatcher } class moduleclass extends module[mymodel, mydispatcher] { //... } class myspecificmoduleclass extends myspecificmodule[moduleclass] { override def dispatcher(): mydispatcher = mydispatcher() }
i don't understand scala's reasoning here, if rid of type parameters, things start work:
case class mymodel(foo: string = "bar") case class mydispatcher() trait module { type dispatcher type model } trait myspecificmodule[a <: module] { def dispatcher(): a#dispatcher } class moduleclass extends module { type model = mymodel type dispatcher = mydispatcher //... } class myspecificmoduleclass extends myspecificmodule[moduleclass] { override def dispatcher(): mydispatcher = mydispatcher() }
and if really want have type params, can introduce helper trait:
trait abstractmodule[m, d] extends module { type model = m type dispatcher = d } class moduleclass extends abstractmodule[mymodel,mydispatcher]
Comments
Post a Comment