I have this trait with a single method of type WSResponse => HTTPCallResult[A]
trait Parsable[A] {
def parse(response: WSResponse): HTTPCallResult[A]
}
I want to define an implicit conversion from a function WSResponse => A
object Parsable {
implicit class DefaultResponseParser[A](parser: WSResponse => A) extends Parsable[A] {
override def parse(r: WSResponse): HTTPCallResult[A] = {
//implementation
}
}
}
The desired behaviour is such, that if a function WSResponse => HTTPCallResult[A]
is passed the anonymous class of Parsable[A] is automatically created. If a function of any other return type is passed (WSResponse => A
) the implicit conversion is triggered.
However, an error occurs when compiling with SBT
[error] /Auth.scala:24:60: type mismatch;
[error] found : String
[error] required: HTTPCall.HTTPCallResult[?]
[error] (which expands to) scala.util.Either[String,?]
[error] .post((r: WSResponse) => (r.json \ "key").as[String])
This function was used in order to test it.
def post[A](parser: Parsable[A])
.post((r: WSResponse) => (r.json \ "key").as[String])
EDIT 1:
Even making the implicit class an implicit method and moving it to the very same scope as post
didn't help.
EDIT 2: It seems that the problem stems from the use of trait with single undefined method. Therefore when passed a function to the post method, the compiler tries to create an anonymous class of type Parser[A] from that. Due to a type missmatch it throws, without even trying to be looking for any implicits.