开发者

programmatically setting the `type` of an abstract type

class MyModel(var username:String, var password:String) e开发者_JS百科xtends FrameworkModel 

object MyModelQuery extends FrameworkQuery { 
  type T = MyModel 
} 

trait FrameworkQuery { 
type T 
//do something with that type 
} 

So I get a class and an object where the latter is mixing in a trait which is defined as an abstract type. Is there a way I could programmatically set the type to the type of MyModel class, so the client would not need to? ie "object MyModelQuery extends FrameworkQuery" would take care of it


Could you achieve a similar effect by just nesting the query in the model?

trait FrameworkModel {
  val model = this
  trait FrameworkQuery {
    type T = model.type
    def getModel: T = model
  }
}

class UserModel extends FrameworkModel {
  // model stuff...
  object UserQuery extends FrameworkQuery {
    // query stuff... 
  }
}


trait T{
    type X = this.type
    def x: X = this
}
object A extends T{
    def b = "Yep"
}    
scala> A.x.b
res0: java.lang.String = Yep
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜