witchcrafters / type_class

(Semi-)principled type classes for Elixir
https://hex.pm/packages/type_class
MIT License
140 stars 16 forks source link

`equal?` as protocol to implement ? #51

Open asmodehn opened 1 year ago

asmodehn commented 1 year ago

I recently had an issue where floats where embedded into a struct, and checking with equal? on the whole struct didn't round the float (as it does on one float). I also don't want to have a property defined in a typeclass, with a specific notion of equality that depends on the implementation...

I am thinking that equal? should be defined in a protocol, and implementable for any struct, just like generator is ? What do you all think ?

asmodehn commented 1 year ago

So, I tried with a protocol: https://github.com/witchcrafters/type_class/commit/7c15ee21aa16023d40153219e480d38d146e7ca0 but this prevents witchcraft from compiling, not sure why.

I implemented equal to be recursive on data structures, and this satisfies my needs so far: https://github.com/witchcrafters/type_class/commit/2278359d684da30f42cc84999681ecba14954213

I ll keep an eye out for any improvements I might need to do on this...