Crell / enum-comparison

A comparison of enumerations and similar features in different languages
82 stars 7 forks source link

Weak comparisons of enums with associated values #17

Closed bwoebi closed 3 years ago

bwoebi commented 4 years ago

When are they equal? When their contents match weakly? strictly?

iluuu1994 commented 4 years ago

I would say when they're the same case and all the members also equate with ==. Similarly, === should match when they're the same case and the members equate with === (like Option::Some(1) === Option::Some(1)).

Crell commented 4 years ago

If we could do that sort of deeper definition of ===, I'm on board. Recall though that it may end up being recursive, since you can easily have an Enum var as a property of another Enum.

TysonAndre commented 3 years ago

If, in the final design, the associated values are guaranteed to be constant expression values (including enums): Maybe the same functionality as WeakMap could be used to ensure that only one instance of Option::Some(1) can exist at a given point of time with a given set of values, making === work by default. using the combination of enum name, case, and params could generate a unique key with serialize or a more compact equivalent (or even using serialize($value) as a key). (or a WeakMap per enum class)

(A binary serialization format may be more efficient, but that's a separate issue)

iluuu1994 commented 3 years ago

If, in the final design, the associated values are guaranteed to be constant expression values (including enums)

That would make them a lot less useful. I think the way to go is to override the === behavior for enums specifically. But we'll deal with this once enums are accepted.