0

I am writing an http client and this is my signature:

def post[Req, Resp](json: Req)(implicit r: Reads[Resp], w: Writes[Req]): Future[Resp]

Using play json behind the scenes.

When I use it like this

def create(req: ClusterCreateRequest): Future[ClusterCreateResponse] = endpoint.post(req)

I get the following error

diverging implicit expansion for type play.api.libs.json.Reads[Resp]

The following works

def create(req: ClusterCreateRequest): Future[ClusterCreateResponse] = endpoint.post[ClusterCreateRequest, ClusterCreateResponse](req)

Why is type inference not working as expected? What can I do for this?

prongs
  • 8,944
  • 19
  • 61
  • 104
  • Have you tried if adding of `(implicit r: Reads[Resp], w: Writes[Req])` to the `create` signature works for your case? – Andriy Plokhotnyuk Apr 08 '20 at 09:51
  • Probably without explicit type parameters some type is inferred to be `Nothing`. And trying to resolve instance of a type class for `Nothing` leads to "diverging implicit expansion". – Dmytro Mitin Jul 06 '20 at 11:49

1 Answers1

-1

diverging implicit expansion for type play.api.libs.json.Reads[Resp] means that Resp has few JSON serializers that are not shadowed one by another.

It's not possible to pinpoint root cause the issue and say fix X and everything will work from the infrmation given in post.

But you can try to "debug" implicit search. Consider checking the implicit search order: Where does Scala look for implicits? Enabling implicit parameter expansion in idea might help to check which implicits(Ctrl+Shift+=) cause a clash.

General advice for type class instances - hold them organized and declared, put them to companion object or to specially dedicated object.

Iva Kam
  • 816
  • 2
  • 12
  • *"`diverging implicit expansion for type play.api.libs.json.Reads[Resp]` means that Resp has few JSON serializers that are not shadowed one by another."* I guess this is incorrect. Having few serializers would cause "ambiguous implicit values" error, not "diverging implicit expansion" error. – Dmytro Mitin Jul 06 '20 at 11:45