rfc:treat_enum_instances_as_values
Differences
This shows you the differences between two versions of the page.
Next revisionBoth sides next revision | |||
rfc:treat_enum_instances_as_values [2023/04/28 21:42] – created suitespacernd | rfc:treat_enum_instances_as_values [2023/04/28 21:44] – suitespacernd | ||
---|---|---|---|
Line 25: | Line 25: | ||
Consider the following enum and array: | Consider the following enum and array: | ||
- | enum Color: int { | + | enum Color: int { |
- | case red = 1; | + | |
- | case green = 2; | + | |
- | case blue = 3; | + | |
- | } | + | |
- | enum WeekDays: int { | + | enum WeekDays: int { |
- | case monday = 1; | + | |
- | case tuesday = 2; | + | |
- | case wednesday = 3; | + | |
- | } | + | |
- | $palette = [ | + | $palette = [ |
- | 1 => ' | + | |
- | 2 => ' | + | 2 => ' |
- | 3 => ' | + | |
- | ]; | + | |
Currently, the following code produces an error: | Currently, the following code produces an error: | ||
- | Color $red = Color:: | + | Color $red = Color:: |
- | $paint = $palette[$red]; | + | |
With this proposal, the code above should not produce an error and should work as expected: | With this proposal, the code above should not produce an error and should work as expected: | ||
- | Color $red = Color:: | + | Color $red = Color:: |
- | $paint = $palette[$red]; | + | |
Last on a more opinionated note; the author suggests that enum cases are userland symbols and should imply value. Just as there is no need in the following scenario: | Last on a more opinionated note; the author suggests that enum cases are userland symbols and should imply value. Just as there is no need in the following scenario: | ||
- | $a = 1; | + | $a = 1; |
- | $b = ' | + | |
- | $list = [ | + | $list = [ |
- | $a => 'some value', | + | |
- | $b => $some_value; | + | |
- | ]; | + | |
vs | vs | ||
- | $list = [ | + | $list = [ |
- | $a-> | + | |
- | $b-> | + | |
- | ]; | + | |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 108: | Line 108: | ||
Future RFCs are recommended to enable | Future RFCs are recommended to enable | ||
- | Day::Monday == Color:: | + | Day::Monday == Color:: |
- | Day::Monday === Color::Red //false | + | |
Which could in turn allow | Which could in turn allow | ||
- | class MyType implements ArrayAccess{ | + | |
+ | class MyType implements ArrayAccess | ||
//... | //... | ||
- | public function offsetGet(Month $which){ | + | |
- | //... | + | |
- | | + | } |
//... | //... | ||
- | } | + | } |
Making the desirable | Making the desirable | ||
- | $myobj = new MyType(); | + | $myobj = new MyType(); |
- | $x = $myobj[Color:: | + | |
Such a roadmap finally enables users to user to simply use match($this) and other userland syntactic sugar to simulate object keys. This gap can be further reduced and optimized from there. | Such a roadmap finally enables users to user to simply use match($this) and other userland syntactic sugar to simulate object keys. This gap can be further reduced and optimized from there. |
rfc/treat_enum_instances_as_values.txt · Last modified: 2023/04/29 11:04 by suitespacernd