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
rfc:treat_enum_instances_as_values [2023/04/28 21:42] – created suitespacerndrfc:treat_enum_instances_as_values [2023/04/29 11:04] (current) – typo correction suggested by internals suitespacernd
Line 1: Line 1:
-====== PHP RFC: Your Title Here ======+====== PHP RFC: Treat Enum Instances as Values  ======
   * Version: 0.1   * Version: 0.1
   * Date: 2023-04-28   * Date: 2023-04-28
Line 11: Line 11:
 This RFC proposes to allow the use of enum instances as array keys in PHP. More specifically, where errors are produces in any context where an enum instance is used without ->value or ->name, the engine should instead assume ->value. This RFC proposes to allow the use of enum instances as array keys in PHP. More specifically, where errors are produces in any context where an enum instance is used without ->value or ->name, the engine should instead assume ->value.
  
-This change aims to enhance the usability and consistency of enums in PHP by treating enum instances as symbols of their respective values - especially in the context of array keys. This will allow large amounts of existing code in various C-style languages to be compatible with PHP usage without affecting current projects. Further, this enables potential future RFC to enable array key type constraints in line with present == vs === semantics and ArrayAccess, without drastic engine overhauls.+This change aims to enhance the usability and consistency of enums in PHP by treating enum instances as symbols of their respective values - especially in the context of array keys. This will allow large amounts of existing code in various C-style languages to be compatible with PHP usage without affecting current projects.  
 + 
 +Further, this proposal provides potential future RFC a path to enable array key type constraints in line with present == vs === semantics and ArrayAccess, without drastic engine overhauls.
  
  
Line 25: Line 27:
 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 90: Line 92:
 Could be a toggle if community desired Could be a toggle if community desired
  
-===== Open Issues ===== 
-Make sure there are no open issues when the vote starts! 
- 
-===== Unaffected PHP Functionality ===== 
-List existing areas/features of PHP that will not be changed by the RFC. 
- 
-This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. 
  
 ===== Future Scope ===== ===== Future Scope =====
Line 108: Line 103:
 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){ 
-      //...+         //... 
 +      
 +    
 +   //...
    }    }
 +   class YourType implements ArrayAccess
 +   //...
 +   
 +      public function offsetGet(mixed $which){
 +         //...
 +      }
        
    //...    //...
-}+   }
  
 Making the desirable Making the desirable
  
-$myobj = new MyType(); +   $myobj = new MyType(); 
-$x = $myobj[Color::Yellow];   // error+   $yourobj = new YourType(); 
 +   $x = $myobj[Color::Yellow];   // error 
 +   $y = $yourobj[Color::Yellow]; // fine
  
 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 132: Line 139:
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Include these so readers know where you are heading and can discuss the proposed voting options.+A two-thirds majority is required for this proposal to be accepted
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 138: Line 145:
  
 ===== Implementation ===== ===== Implementation =====
-Author's org has committed work on this issue and have it implemented, assuming community is in favor and this is not trivial for an existing member. Any guidance from those who have worked previously with enums is appreciated.+Author's org has committed work on this issue and having it implemented, assuming community is in favor and this is not trivial for an existing member. Any guidance from those who have worked previously with enums is appreciated.
  
  
 ===== 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.1682718143.txt.gz · Last modified: 2023/04/28 21:42 by suitespacernd