Stackoverflow when implicitly creating an EntityEncoder for an implicit circe Encoder
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:4
- Comments:14 (14 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
forA
, and anEntityEncoder[F, String]
. So first of all your signature above is making the mistake of not propagating theEntityEncoder[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 defineentitytEncoder
you calljsonEncoder
, which looks for anEntityEncoder[F, String]
and finds…entityEncoder
(by applyingA
toString
), and therefore the code compiles.At runtime, calling
entityEncoder
will evaluatejsonEncoder
, which will evaluateentityEncoder
, which will evaluatejsonEncoder
, which will evaluate… and so on.This infinite recursion causes a stack overflow.
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 anEntityEncoder
for anobject
though but it’s good to keep this in mind.