question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stackoverflow when implicitly creating an EntityEncoder for an implicit circe Encoder

See original GitHub issue

I brought this up in the gitter channel and received great help from @SystemFw.

I’m adding it as an issue so that other people who might stumble over it are warned and also for tracking a possible fix for it.

If you create an implicit EntityEncoder from an implicit Encoder like so:

implicit def entityEncoder[A: Encoder]: EntityEncoder[F, A] = jsonEncoderOf[F, A]

You will get a StackOverflowError.

The solution (thanks @SystemFw) is to define the implicit like this:

  implicit def entityEncoder[A](implicit enc:  Encoder[A], ev: EntityEncoder[F, String]): EntityEncoder[F, A] = jsonEncoderOf[F, A](ev, implicitly[Applicative[F]], enc)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
SystemFwcommented, Mar 29, 2018

@kevinmeredith sure. Let’s look at the signature of jsonEncoderOf: def jsonEncoderOf[F[_]: EntityEncoder[?[_], String]: Applicative, A: Encoder]: EntityEncoder[F, A]

It wants an Encoder for A, and an EntityEncoder[F, String]. So first of all your signature above is making the mistake of not propagating the EntityEncoder[F, String] constraint. Normally forgetting to propagate an implicit will cause a compilation error, since the method call needing it will not find it, however in this specific case, to define entitytEncoder you call jsonEncoder, which looks for an EntityEncoder[F, String] and finds…entityEncoder (by applying A to String), and therefore the code compiles.

At runtime, calling entityEncoder will evaluate jsonEncoder, which will evaluate entityEncoder, which will evaluate jsonEncoder, which will evaluate… and so on.

This infinite recursion causes a stack overflow.

1reaction
gvolpecommented, Mar 14, 2018

Normally I define my sealed traits that are ADT’s by extending Product with Serializable to help the type inference system. Not sure when one would try to get an EntityEncoder for an object though but it’s good to keep this in mind.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circe: Moving an Implicit Encoder into A Generic Class
So my question is, how would I get the encoder that is defined implicitly into scope for the Class. Any answers with an...
Read more >
http4s/http4s - Gitter
I'm still having trouble understanding why the following results in a StackOverflowError for List[String] implicit def entityEncoder[A: ...
Read more >
Encoding and decoding - circe
circe uses Encoder and Decoder type classes for encoding and decoding. ... value to either an exception or an A . circe provides...
Read more >
Circe is driving me insane : r/scala - Reddit
I have this piece of code object DefaultValues { implicit val useDefaultValues = Configuration.default } import DefaultValues.
Read more >
Read Pure functional HTTP APIs in Scala | Leanpub
_ 2 import io . circe . generic . semiauto . _ 3 4 implicit val decode : Decoder [ Product ] =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found