nrinaudo / dsl-tagless

https://nrinaudo.github.io/dsl-tagless/
4 stars 0 forks source link

Asymmetry between encoding and decoding #4

Open jmgimeno opened 2 years ago

jmgimeno commented 2 years ago

The Json library does some magic behind the scenes. I'm referring to the fact that the Json.Arr factory method calls encode behind the scenes.

This kills the asymmetry in the implementation of encoding (https://nrinaudo.github.io/dsl-tagless/#245) and decoding (https://nrinaudo.github.io/dsl-tagless/#252).

Moreover, in https://nrinaudo.github.io/dsl-tagless/#245 we pass Json.Arr the unencoded parameters but in https://nrinaudo.github.io/dsl-tagless/#511 it seems, if I'm not mistaken, that we pass the parameters already encoded.

nrinaudo commented 2 years ago

That's a design choice.

I agree that it's good to be explicit about everything in a presentation - with one qualifier: everything that matters. The details of JSON encoding and decoding do not, but removing as much of the useless boilerplate as possible does.

If you look at the rest of the talk, I will highlight what matters very explicitly, and leave a lot of code unexplained. What's even better than unexplained code is... code that is not there.

I also tried to make it symmetrical, but unfortunately, this is apparently not feasible in Scala 3, or at least not sanely.

As for your second point: yes, we get the value already encoded. That's the entire point. You get the fully evaluated version of your rule both on the lhs and rhs, and the fully evaluated version is a JSON object.

jmgimeno commented 2 years ago

My problem was that, if you pass the values already encoded, as in the Json.Arr apply method you do Json.Arr(as.map(_.asJson).toList), you are encoding the parameters twive. But I've found that you have an identity encoder for Json as well.

Thanks for your patience.

nrinaudo commented 2 years ago

No, thanks for the time you're sinking into this, it's very helpful!

One thing that might not be clear though: it's absolutely guaranteed that all the code you see compiles and run, because that's how the slides are built: all the Scala code is injected into a REPL session. If anything was illegal, the slides wouldn't built.