How to define a function whose output type depends on the input type
Given the following classes: case class AddRequest(x: Int, y: Int) case class AddResponse(sum: Int) case class ToUppercaseRequest(str: String) case class ToUppercaseResponse(upper: String) How do I define in a typesafe manner some function: def process(req: ???): ??? Such that the following should hold true: val r1: AddResponse = process(AddRequest(2, 3)) val r2: ToUppercaseResponse = process(ToUppercaseRequest("aaa")) Also, the following should not compile: val r3 = process("somestring") This is both entirely possible and a totally reasonable thing to do in Scala. This kind of thing is all