rfc:enum_v2

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:enum_v2 [2020/05/16 13:18] – ++ maxsemrfc:enum_v2 [2021/02/18 13:14] (current) – Move status to obsolete ilutov
Line 3: Line 3:
   * Date: 2020-05-14   * Date: 2020-05-14
   * Author: Max Semenik, maxsem.wiki@gmail.com   * Author: Max Semenik, maxsem.wiki@gmail.com
-  * Status: Draft+  * Status: Obsolete
   * First Published at: http://wiki.php.net/rfc/enum_v2   * First Published at: http://wiki.php.net/rfc/enum_v2
 +
 +//Note: this is a counterproposal to [[rfc:enum|this RFC]]//
  
 ===== Introduction ===== ===== Introduction =====
Line 23: Line 25:
 ===== Proposal ===== ===== Proposal =====
  
-=== Basics ===+==== Basics ====
 A simple enum: A simple enum:
 <code php> <code php>
Line 74: Line 76:
     baz = 1.5       // CompileError     baz = 1.5       // CompileError
 } }
 +
 +$x = 'this is a string';
 +$y = (foo)$x; // TypeError
 </code> </code>
  
-=== Type coercion and casts ===+==== Type coercion and casts ====
 Enums are implicitly coercible to bool and string: Enums are implicitly coercible to bool and string:
 <code php> <code php>
Line 88: Line 93:
 </code> </code>
  
-=== Enum operations ===+Enum types can be explicitly cast to each other and ''int'': 
 +<code php> 
 +$foo = (FileMode)123; 
 +$bar = SomeEnum::Const; 
 +$foo = (FileMode)$bar; 
 +</code> 
 + 
 +Conversion from other types is not checked, thus enums can hold values not covered by their constants. ''Enum::isKnownValue()'' will return `false` while ''Enum::toHumanReadableString()'' will return a numeric string instead of constant name(s). 
 + 
 +==== Enum operations ====
 Enums are immutable and don't support arithmetic operations: Enums are immutable and don't support arithmetic operations:
 <code php> <code php>
-$foo = +$foo = FileMode::Read; 
 +$foo = FileMode::Read + 1; // CompileError 
 +$foo += 1;                 // TypeError 
 +$bar = $foo + 1;           // TypeError 
 +</code> 
 + 
 +However, binary enums support bitwise operations: 
 +<code php> 
 +$foo = FileMode::Read | FileMode::Execute; 
 +$foo |= FileMode::Write; 
 +$foo &= ~FileMode::Read;
 </code> </code>
  
-=== Enum usage ===+==== Enum usage ====
 Concrete enum types can be used as typehints: Concrete enum types can be used as typehints:
 <code php> <code php>
Line 125: Line 149:
 </code> </code>
  
-=== Internal representation ===+==== Internal representation ====
 Internally, enums are classes and enum constants are public class constants. This makes them the fourth OOP-ey type in PHP, along with ''class'', ''interface'' and ''trait''. They can be autoloaded just like the former types. All enums inherit from this base class (here is PHP pseudocode): Internally, enums are classes and enum constants are public class constants. This makes them the fourth OOP-ey type in PHP, along with ''class'', ''interface'' and ''trait''. They can be autoloaded just like the former types. All enums inherit from this base class (here is PHP pseudocode):
 <code php> <code php>
Line 143: Line 167:
     // Returns a human readable representation of this enum's value     // Returns a human readable representation of this enum's value
     // e.g. (FileMode::Read | FileMode::Write)->toHumanReadableString() would return 'Read | Write'     // e.g. (FileMode::Read | FileMode::Write)->toHumanReadableString() would return 'Read | Write'
 +    // For unrecognized values, returns a decimal (simple enums) or hexadecimal (binary enums) string.
     public function toHumanReadableString(): string;     public function toHumanReadableString(): string;
 +    
 +    public static function parse(string $enum) : ?WhateverConcreteEnumTypeIsExtendingThis;
 } }
 </code> </code>
  
 +===== Conventions used in this document =====
 +Currently, PascalCase is used in enums due to author's experience with C#. While I believe that this convention is nice and makes enums conveniently distinct from PHP conventions that use camelCase for properties and UNDERSCORED_UPPERCASE for constants, I'm not attached to it. The recommended convention for use in language documentation and, subsequently, the PHP core will be determined during community discussion or voted for during the voting phase.
 +
 +Same applies to the ''Enum'' class name.
  
 ===== Backwards Incompatible Changes ===== ===== Backwards Incompatible Changes =====
Line 156: Line 187:
 ===== Open Issues ===== ===== Open Issues =====
 Make sure there are no open issues when the vote starts! Make sure there are no open issues when the vote starts!
 +
 +  * Naming conventions
 +  * Base class name(s)
 +  * Type coercion details?
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
Line 163: Line 198:
  
 ===== Future Scope ===== ===== Future Scope =====
-This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.+After this RFC is implemented, enums may be used for new features or factored into existing ones.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-* Accept this RFC (yes / no)? +  * Accept this RFC (yes / no)? 
-* What should be Enum fully qualified name (\Enum / \PHP\Enum)? +  * What should be enum base class fully qualified name (''\Enum'' ''\PHP\Enum'' / something else )? 
-* What enum constant naming convention should be used (PascalCase / camelCase / UPPER_UNDERSCORED)?+  * What enum constant naming convention should be used (PascalCase / camelCase / UPPER_UNDERSCORED)?
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 188: Line 223:
 ===== References ===== ===== References =====
 Links to external references, discussions or RFCs Links to external references, discussions or RFCs
-* https://wiki.php.net/rfc/enum+* https://wiki.php.net/rfc/enum - old proposal that wanted to introduce 
  
 ===== Rejected Features ===== ===== Rejected Features =====
 Keep this updated with features that were discussed on the mail lists. Keep this updated with features that were discussed on the mail lists.
rfc/enum_v2.1589635103.txt.gz · Last modified: 2020/05/16 13:18 by maxsem