Crell / enum-comparison

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

dynamic "match type" #18

Closed bwoebi closed 3 years ago

bwoebi commented 4 years ago

What are accepted values on the LHS of =>? Is that supposed to work like instanceof, e.g. match type ($foo) { $enumclassname1 => ...,$enumclassname2 => ..., } would be valid? And generally applicable to all objects / types (e.g. int, stdClass etc.)

iluuu1994 commented 4 years ago

I'm not a big fan of the syntax honestly. I'd prefer more traditional pattern matching. I'd also argue this belongs to the pattern matching RFC more than the enum RFC.

bwoebi commented 4 years ago

Yes, I think pattern matching would solve that too, hence #13 to see what they shall and can do.

Crell commented 4 years ago

My thinking there was exactly that. match type would mean "do an instanceof if it's an object or enum, and a get_type() for anything else." So you could then, mostly as a side effect, do:

match type ($var) {
  string => "It was a string",
 array => "It was an array",
};

Whether that's ideal or not, I don't know. It's just the best way I could come up with to make match handle Associable Case Enums without including pattern matching. Although even then we may need to tweak match, as $some_value === $a_pattern_definition isn't quite cricket, either.

iluuu1994 commented 3 years ago

We don't really need this for now.