playframework 2.0 - Scala Type Mismatch issue when using import from different scope -
in project(play 2.4 slick 3.0), have setup: using mysql production , h2 unit testing(start feel horrible idea). that, have import slick.driver.jdbcprofile
instead of specific driver (e.g. slick.driver.mysqldriver
) , import api._
in dao class, this:
class somedao(val context: mycontext){ import context.profile.api._ import context.some_type def invoke(para: some_type) = { ... } // other dbio... }
everything ok far, however, have service class requires dao, , type in context parameter:
class myservice(val context: mycontext){ val somedao = new somedao(context) import somedao.context.some_type // works // import context.some_type // type mismatch! how avoid it??? def invokedaomethod(para: some_type) = somedao.invoke(para) // mismatch occurred // expect: myservice.this.somedao.context.some_type // actual: myservice.this.context.some_type }
problem heppens when try import type exact same context
instance, intuitively speaking, i'm using "same" type here, right?
can explain kind of behaviour , give hint patterns cut kind of 'non-sense' wrote?
you using path-dependent types. literally dependent on paths, not on content.
consider example:
class store { case class box[t](box : t) def box[t](b : t) = box[t](b) def unbox[t](b : box[t]) : t = b.box } object assign { val x = new store() val y = x val box = x.box[int](2) val ub = y.unbox[int](box) }
you may assume naively x
, y
practically identical, share same content , same type. true content. wrong types. , compiler nicely provide type error:
error: type mismatch;
found : dependent.assign.x.box[int]
required: dependent.assign.y.box[int]
but tell compiler x
, y
should share same path-dependent types despite being different literal paths
object assign { val x = new store() val y : x.type = x val box = x.box[int](2) val ub = y.unbox[int](box) }
with val y : x.type = x
compiler success.
your issue has similar nature, , solution similar too: should specify type equivalence explicitly. change samedao definition accept type parameter
class somedao[mc <: mycontext](val context: mc){ ... }
and pass appropriate type upon creation:
class myservice(val context: mycontext){ val somedao = new somedao[context.type](context) ... }
Comments
Post a Comment