valentiay / phobos

Efficient and expressive XML data-binding library for Scala
Apache License 2.0
20 stars 5 forks source link

Add encoders for Nothing #9

Closed geny200 closed 4 months ago

valentiay commented 6 months ago

Hi! Could you please provide a use case for an encoder for Nothing? Such encoder would violate the laws for Encoder/Decoder, because x == decode(encode(x)) where x is of type Nothing would not compile, because a decoder for Nothing does not exist. On the other hand, one cannot run such code, because it is impossible to create an instance of Nothing. Instances for Nothing are strange, but I don't mind adding them, if there is a need

geny200 commented 6 months ago

Could you please provide a use case for an encoder for Nothing?

Hi, I have a strange case, but still, for example, take Either, which can only have Right values (but only in our part of the service, in other places it can also be Left). You will need Nothing encoder for it.

yes, it is a fair remark that the law x == decode(encode(x)) is not fulfilled, but there is a slight difference, when encoding we know more information, and we can guarantee that there will be a certain type. When decoding, you need to decode the general type.

valentiay commented 6 months ago

Hi, I have a strange case, but still, for example, take Either, which can only have Right values (but only in our part of the service, in other places it can also be Left). You will need Nothing encoder for it.

Wouldn't creating an encoder for Right / Left solve this problem? It seems like a more straightforward and explicit approach

geny200 commented 6 months ago

Wouldn't creating an encoder for Right / Left solve this problem?

Not really, especially when you derive implicits for own types, and hide the fact that Right/Left is used inside. I was inspired by the Loggable instance from tofu (Loggable for Nothing).

creating an encoder for Right / Left

By suggesting this, you have raised another problem, that we can't write a common encoder and decoder for Either, so based on this, we cannot write common codecs for Left and Right.

valentiay commented 4 months ago

Hi! I apologize for the delay in my response. To be honest, I still don't fully understand your use case for Nothing. These instances seem a bit strange to me, but I couldn't find any issues that it might cause. Therefore, I will merge the PR