I wouldn't recommend having the default operators delegate to such a protocol, rather just a `cmp` or `compare` or something call, along with a warning about using the operators on a struct if it is statically known that it is a struct (not too uncommon).
On Friday, October 19, 2018 at 4:50:02 PM UTC-6, Sheharyar Naseer wrote: > > Struct comparisons already throw a warning > <https://github.com/elixir-lang/elixir/blob/741dfda9f4be898c647426493f1d17fb9a4b9c53/lib/elixir/src/elixir_expand.erl#L1161-L1165> > > in Elixir, as it does not make a lot of sense to compare them (except in > some very specific cases like date/time): > > warning: invalid comparison with struct literal ~N"2018-10-19 >> 21:35:18.005355". Comparison operators (>, <, >=, <=) perform structural >> and not semantic comparison. Comparing with a struct literal is unlikely to >> give a meaningful result. Modules typically define a compare/2 function >> that can be used for semantic comparison >> > > But currently this happens only when literal structs are used. For > example, when using `Enum.sort` to sort a list of dates, this warning is > not raised. I know there already has been some discussion on this > <https://groups.google.com/forum/#!topic/elixir-lang-core/eE_mMWKdVYY>, > but I believe OvermindDL's suggested approach > <https://github.com/elixir-lang/elixir/pull/7274#issuecomment-361362861> > to have a Comparable protocol is much better. Kernel's comparison operators > would delegate to this protocol, with implementations for common things > like Date/Time, and letting it simply throw Protocol.UndefinedError for > structs which do not have explicitly implemented it. > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/d3d1dad2-c713-4c73-90a3-acfabe190910%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.