2

I am trying to call function to marshal HttpResponse from spray. unmarshal function described here. response is HttpResponse

Consider 3 code variants:

First

val func = unmarshal[MyType]
func(response)

Second

unmarshal[MyType].apply(response)

Third

unmarshal[MyType](response)

Why does my third code variant does not compile while first two are works? The compiler returns:

[error]  found   : spray.http.HttpResponse
[error]  required: spray.httpx.unmarshalling.FromResponseUnmarshaller[MyType]
[error]     (which expands to)  spray.httpx.unmarshalling.Deserializer[spray.http.HttpResponse,MyType]
[error]         unmarshal[MyType](response)

Is there a way to call function returned by unmarshal more elegant then create temp variable or direct call apply method?

Raziel
  • 444
  • 6
  • 22
Cherry
  • 31,309
  • 66
  • 224
  • 364

1 Answers1

4

The signature of that function is (from your link):

  def unmarshal[T: FromResponseUnmarshaller]

so the T needs implicit evidence that there's such a FromResponseUnmarshaller for it. The signature actually compiles to something like:

 def unmarshal[T](implicit evidence$1: FromResponseUnmarshaller[T]): HttpResponse ⇒ T

That means, the unmarshal function actually takes also an implicit parameter that should transform your MyType to an HttpResponse.

In your first example, you call val func = unmarshal[MyType] which makes the compiler insert the implicit for you. In your 3rd example,

 unmarshal[MyType](response)

response is taking the position of the implicit parameter, which is supposed to be a FromResponseUnmarshaller, not a HttpResponse.

Such a call would need to be:

unmarshal[MyType](fromResponseUnmarshsaller)(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^
       This is the original method call          Here you apply response to the returned function.
Chirlo
  • 5,989
  • 1
  • 29
  • 45
  • I do not see any of implicit parameters in line `def unmarshal[T: FromResponseUnmarshaller]: HttpResponse ⇒ T` from `unmarshal` function source. Could please add more details about implicit parameters for `unmarshal` function? – Cherry Aug 25 '15 at 08:41
  • @Cherry, the parameter is included there by defining `[T : FromResponseUnmarshaller]`. Try creating a function on the REPL, like `def f [T: List] = 0`, you'll see that the REPL defines it as `f: [T](implicit evidence$1: List[T])Int`. For more info, check @dcastro's link – Chirlo Aug 25 '15 at 08:48
  • Thanks. It seems there is no way to skip implicit context bound parameters from call statement. :( – Cherry Aug 25 '15 at 09:20