1

I have the following setup:

trait TypeA { override def toString() = "A" }
trait TypeB { override def toString() = "B" }
trait TypeC { override def toString() = "C" }

def foo[T](t: T) = println(t)

Now I can do something like this:

val valueB: Any = new TypeB {}

val typedValue = valueB match {
  case t: TypeA => foo(t)
  case t: TypeB => foo(t)
  case t: TypeC => foo(t)
}
// prints "B"

If I want to generalize this pattern matching block, I can simply do:

val typedValue = valueB match {
  case t => foo(t)
}

and it will work. However, in my real use case I need to explicitly state the type information when invoking the method because there is no function argument to infer it from. So if foo() was a generic method parameterized with type parameter T, but without actual parameters of that type to infer from, can I generalize this into a pattern matching with just one case statement (probably using the Reflection API)?

So, how to generalize this?

val typedValue = valueB match {
  case t: TypeA => foo[TypeA]()
  case t: TypeB => foo[TypeB]()
  case t: TypeC => foo[TypeC]()
  ...
}
2
  • 2
    This appears to compile: case t => foo[t.type]() Give it a try.
    – jwvh
    Aug 3, 2016 at 8:43
  • The tough part is, how to make the pattern matching really extract the actual type? I would like to be able to say val t: Any = ... and in the pattern matching part I would match on the actual type. See the edited question (val valueB is now of type Any). If valueB is declared to be of type Any, your suggestion unfortunately doesn't work, and with a weird message too (unspecified value parameters: t.type).
    – slouc
    Aug 3, 2016 at 12:54

1 Answer 1

1

If I want to generalize this pattern matching block, I can simply do:

val typedValue = valueB match {
  case t => foo(t)
}

In general you can't. E.g. if foo(x: TypeA), foo(x: TypeB) and foo(x: TypeC) are separate overloads. And that's the situation for your real code: you'd have to write separate methods for JsObject, etc. because those value calls just happen to have the same name; you can't write foo(x: JsValue) or foo[T <: JsValue](x: T) which would do what you want (without the same match which you want to avoid).

In the case where you do have a single polymorphic method: because generic arguments get erased, if you have def foo[T]() = ..., foo[TypeA](), foo[TypeB]() and foo[TypeC]() will execute the same actual code (this doesn't apply to classOf, isInstanceOf or asInstanceOf, but those are the only exceptions and it's because they aren't really generic methods). So you can just call foo[<type-of-valueB>]. For them to be different, foo has to have an implicit argument which depends on T, e.g.

trait Baz[A] { ... }
object Baz {
  implicit val bazTypeA: Baz[TypeA] = ...
  ...
}

def foo[A]()(implicit baz: Baz[A]) = ...

In this case the way to avoid branching is for the method calling foo to accept the same implicit:

def bar[A](value: A)(implicit baz: Baz[A]) = foo[A]()

bar(new TypeA) // uses bazTypeA
2
  • Can you explain how does this work? It's a snippet using Play JSON; if you're not familiar with it, you'll get the point anyway.. Isn't JsNumber and JsString resolved at runtime and thus should be erased? val result = json match { case jsString: JsString => json.as[JsNumber].value } // doesn't work and val result = json match { case jsString: JsString => json.as[JsString].value } // works One would expect that two calls, as[JsString] and as[JsNumber], trigger the same code like you said. But the type parameter makes a big difference.
    – slouc
    Aug 5, 2016 at 12:40
  • 1
    as uses an implicit argument, which is covered at the end of my answer. Aug 5, 2016 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.