shawnT
shawnT

Reputation: 398

How to infer types when one is a return value?

I'm trying to define a method which is generic both in its parameter and return type. Basically to make a helper function for JSON serialization to/from case classes.

so I want to write something like this pseudocode:

def post[Request,Response](data:Request) : Response = ???

case class A(i:String)
case class B(j:Int)

val result = post[A,B]("input")

in this case (assuming no errors) result is of type B.

It's understandable that the compiler can't infer the return value, but I'd like it to infer the Request type. In other words I'd like to write something like

val result = post[B]("input")

where the type of A is inferred by the data parameter, so the user need only specify the return type when calling the function.

Upvotes: 2

Views: 129

Answers (2)

Shadowlands
Shadowlands

Reputation: 15074

Following on from @amalloy's answer, and the link he provides, the Scala equivalent for what you are trying to achieve would be something like the following:

trait ReqResp[Request,Response] {
  def apply(req: Request): Response
}

def post[Request,Response](data:Request)(implicit rr: ReqResp[Request,Response]): Response = rr(data)

case class A(i:String)
case class B(j:Int)

implicit object reqRespAB extends ReqResp[A,B] {
  def apply(a: A) = B(a.i.toInt)
}

val result = post(A("456"))

This gives the output:

result: B = B(456)

Upvotes: 1

amalloy
amalloy

Reputation: 91857

I don't know many details of Scala specifically, but in Haskell that ability is enabled by a compiler option called "Functional dependencies", whereby you have a typeclass with two type variables, one of which can be derived from the other - see section 7.4.3 of http://www.haskell.org/ghc/docs/6.6/html/users_guide/type-extensions.html. Obviously you can't just use this feature, since it's in a different language, but knowing what it's called should help you find a solution. For example, Functional dependencies in Scala looks like a good guess; although again, I don't know enough Scala to read that article and then tell you exactly how to answer your original JSON question.

Upvotes: 2

Related Questions