banduk
banduk

Reputation: 107

Using scala wildcards for a typed class

This may be a very stupid question, but as I'll try it anyway. I'm trying to use wildcards with a typed class so I can get the properties of it.

I am trying to provide a sintax (DSL-like) to create objects that would treat the incoming message accordingly to it's type parametrization . I'd really like the sintax looks something like this:

// Types of message
trait TypeMessage
case class TypeMessage1 extends TypeMessage{
    val a : String,
    val b : Int
}
case class TypeMessage2 extends TypeMessage{
    val c : Double,
    val d : String
}

// Here is the problem
class TreatMsg[T <: TypeMessage] {
    // I'm using a random sintax here, just for ilustration
    def getParamInfo( variable : ?? ) = { 
        println("name: " + variable.name + "  value: " + variable.val + "  type: " + variable.val.getClass)
    }
}
object TreatMsg{
    def apply[T <: TypeMessage] = new TreatMsg[T]
}


// Creating actors
TreatMsg[TypeMessage1].getParamInfo(_.a)
TreatMsg[TypeMessage2].getParamInfo(_.d)

So, how to get this wildcard working? I mean, I'd like that the "getParamInfo()" function accepts only properties from the class passed to TreatMsg. I have a solution that uses reflection, but I'd like to get rid of this reflection. Any idea?

Thank you!

Upvotes: 0

Views: 438

Answers (1)

Ken Bloom
Ken Bloom

Reputation: 58770

def useVariable[RetVal](func: (T)=>RetVal) = //...

Upvotes: 1

Related Questions