tags:

views:

147

answers:

2
class MyModel(var username:String, var password:String) extends 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

A: 
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
Thomas Jung
Thanks although I do not see how this would work in my situation. this.type would return MyModel object as the type and not MyModel class which i would like to reference. I am going to change the example a bit to make it clearer
poko
You've changed the types, right? I don't see how you to relate MyModelQuery and MyModel.
Thomas Jung
MyModel could be looked up up with reflection and relaying on naming conventions. So I could get a reference to the class of MyModel but then what? So I guess my question is whether there is a way to execute something something similar to this#type but on an arbitrary class?
poko
A: 

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... 
  }
}
Mitch Blevins
the issue with that approach is that client can declare multiple models like this:class MyUserModel (var username:String, var password:String) extends FrameworkModel class MyCommentModel (var comment:String) extends FrameworkModel in each case I would like to pass the model type to the Query trait in the corresponding Query object
poko
I see. Almost looks like you want convention-based static typing. Companion objects are an example of it, but I don't think you can do arbitrary stuff like you are desiring.
Mitch Blevins
Edited answer to reflect my latest understanding of your question.
Mitch Blevins