2
votes

I have a self-recursive type:

trait Problem[P <: Problem[P]] {
  type Solution
}

Now, I want to pattern match on P#Solution. Let's assume we're within the case class Foo[P <: Problem[P]]():

case ExampleCaseClass(s: P#Solution) if conditionApplies() =>
  // do sth

case ExampleCaseClass(s: P#Solution) =>
  // now sth else

Of course it fails due to type erasure. Is there any way to make that code compile in scala?

I've seen class/type tags but I'm not really sure if they can be use in such case.

1

1 Answers

2
votes

You can indeed use type tags, but you need a concrete type to obtain the type tag from. You could add the solution type as type parameter:

case class Foo[P <: Problem[P], S <: P#Solution:TypeTag]() {
  def doSomething[T:TypeTag](c: ExampleCaseClass[T]) = c match {
    case ExampleCaseClass(s) if typeOf[T] =:= typeOf[S] => "x"
    case ExampleCaseClass(s) => "y"
  }
}

If you want a subtype match, use <:< instead of =:=.