0

Hey so I have the following ADT

 sealed trait MessageContext
 case class ThisMessageContext(...) extends MessageContext
 case class ThatMessageContext(...) extends MessageContext

and another class which is

 case class Message[+T <: MessageContext](...)

now I have this method

def doThisOrThat[T <: MessageContext](message: Message[T]) = message match {
    case mThis: Message[ThisMessageContext] = goThisWay(mThis)
    case mThat: Message[ThatMessageContext] = goThatWay(mThat)
}

private def goThisWay(msg: Message[ThisMessageContext]) = { // Some logic here}

private def goThatWay(msg: Message[ThatMessageContext]) = { // Some logic here}

Now I expect that for Message[ThatMessageContext] I am supposed to goThatWay but its evaluating to the first one. In fact due to type erasure its always hitting the first case no matter what I throw at it.

Is there a way to circumvent the type erasure here. AFAIK ClassTag is something I can use, but I do not how that works exactly. Also I do not like the idea of boxing the input.

Arunav Sanyal
  • 1,708
  • 1
  • 16
  • 36

0 Answers0