user79074
user79074

Reputation: 5280

How to implicitly pass class manifest for a return value

Say I define the generic function below:

def convert[R](json: String)(implicit m: Manifest[R]): R =
  parse(json).extract[R]

I am not sure what the correct syntax for calling this function would be? I have tried:

 convert(json).asInstanceOf[MyClass]

Seems to compile correctly but get an exception when I try to extract the json. I can get it to work correctly by defining the following for example:

def convert[R](json: String)(cb: R => Unit)(implicit m: Manifest[R]) =
  cb(parse(json).extract[R])

And then doing the following:

 convert(json) { ret: MyClass => // }

But is not the appropriate solution.

Upvotes: 1

Views: 173

Answers (1)

lmm
lmm

Reputation: 17431

Try convert[MyClass](json); that's the syntax for explicitly specifying a type parameter. Allowing the type to be inferred by putting it in a context where it's given, something like convert(json): MyClass, might also work.

Upvotes: 2

Related Questions