rfc:enum_v2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:enum_v2 [2020/05/16 13:18] – ++ maxsem | rfc: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: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
+ | |||
+ | //Note: this is a counterproposal to [[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 | ||
</ | </ | ||
- | === 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: | ||
</ | </ | ||
- | === Enum operations === | + | Enum types can be explicitly cast to each other and '' |
+ | <code php> | ||
+ | $foo = (FileMode)123; | ||
+ | $bar = SomeEnum:: | ||
+ | $foo = (FileMode)$bar; | ||
+ | </ | ||
+ | |||
+ | Conversion from other types is not checked, thus enums can hold values not covered by their constants. '' | ||
+ | |||
+ | ==== 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:: |
+ | $foo = FileMode:: | ||
+ | $foo += 1; // TypeError | ||
+ | $bar = $foo + 1; // TypeError | ||
+ | </ | ||
+ | |||
+ | However, binary enums support bitwise operations: | ||
+ | <code php> | ||
+ | $foo = FileMode:: | ||
+ | $foo |= FileMode:: | ||
+ | $foo &= ~FileMode:: | ||
</ | </ | ||
- | === 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: | ||
</ | </ | ||
- | === 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 '' | Internally, enums are classes and enum constants are public class constants. This makes them the fourth OOP-ey type in PHP, along with '' | ||
<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:: | // e.g. (FileMode:: | ||
+ | // For unrecognized values, returns a decimal (simple enums) or hexadecimal (binary enums) string. | ||
public function toHumanReadableString(): | public function toHumanReadableString(): | ||
+ | | ||
+ | public static function parse(string $enum) : ? | ||
} | } | ||
</ | </ | ||
+ | ===== Conventions used in this document ===== | ||
+ | Currently, PascalCase is used in enums due to author' | ||
+ | |||
+ | Same applies to the '' | ||
===== 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, |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | * Accept this RFC (yes / no)? | + | |
- | * What should be Enum fully qualified name (\Enum / \PHP\Enum)? | + | * What should be enum base class fully qualified name ('' |
- | * 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:// | + | * https:// |
===== 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