rfc:treat_enum_instances_as_values

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:treat_enum_instances_as_values [2023/04/28 21:42] – created suitespacerndrfc:treat_enum_instances_as_values [2023/04/28 21:45] – cleanup 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 red = 1; 
-    case green = 2; +       case green = 2; 
-    case blue = 3; +       case blue = 3; 
-}+   }
  
-enum WeekDays: int { +   enum WeekDays: int { 
-    case monday = 1; +       case monday = 1; 
-    case tuesday = 2; +       case tuesday = 2; 
-    case wednesday = 3; +       case wednesday = 3; 
-}+   }
  
-$palette = [ +   $palette = [ 
-    1 => 'Red', +       1 => 'Red', 
-    2 => 'Green', +      2 => 'Green', 
-    3 => 'Blue', +       3 => 'Blue', 
-];+   ];
  
 Currently, the following code produces an error: Currently, the following code produces an error:
  
-Color $red = Color::red; +   Color $red = Color::red; 
-$paint = $palette[$red];  // Error+   $paint = $palette[$red];  // Error
  
 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::red; +   Color $red = Color::red; 
-$paint = $palette[$red];  // 'Red'+   $paint = $palette[$red];  // '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 = '2';+   $b = '2';
  
-$list = [ +   $list = [ 
-  $a => 'some value', +     $a => 'some value', 
-  $b => $some_value; +     $b => $some_value; 
-];+   ];
  
 vs vs
  
-$list = [ +   $list = [ 
-  $a->value => 'some value', +     $a->value => 'some value', 
-  $b->value => $some_value; +     $b->value => $some_value; 
-];+   ];
  
 ===== 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::Red  //true +   Day::Monday == Color::Red  //true 
-Day::Monday === Color::Red //false+   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){ +      public function offsetGet(Month $which){ 
-      //... +         //... 
-   }+      }
        
    //...    //...
-}+   }
  
 Making the desirable Making the desirable
  
-$myobj = new MyType(); +   $myobj = new MyType(); 
-$x = $myobj[Color::Yellow];   // error+   $x = $myobj[Color::Yellow];   // error
  
 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.
Line 142: Line 143:
  
 ===== References ===== ===== References =====
-A robust discussion about further pros/cons/considerations at the original issue - https://github.com/php/php-src/issues/9208 +A robust discussion about further pros/cons/considerations at the original issue -  
-Mailing list introduction and opening RFC- https://news-web.php.net/php.internals/120117+https://github.com/php/php-src/issues/9208 
 + 
 +Mailing list introduction and opening RFC-  
 +https://news-web.php.net/php.internals/120117 
 Mailing list post-RFC discussion: TBD Mailing list post-RFC discussion: TBD
  
 ===== Rejected Features ===== ===== Rejected Features =====
 None to date None to date
rfc/treat_enum_instances_as_values.txt · Last modified: 2023/04/29 11:04 by suitespacernd