rfc:tagged_unions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:tagged_unions [2020/12/05 20:21] – crell | rfc:tagged_unions [2024/07/16 14:12] (current) – crell | ||
---|---|---|---|
Line 24: | Line 24: | ||
Tagged cases are mutually exclusive with Scalar Cases. | Tagged cases are mutually exclusive with Scalar Cases. | ||
- | Associated | + | If two Tagged Case instances have the same associated value, the object instance will be reused. |
<code php> | <code php> | ||
enum Distance { | enum Distance { | ||
- | case Kilometers(public | + | case Kilometers(int $km); |
- | case Miles(public | + | case Miles(int $miles); |
} | } | ||
Line 38: | Line 38: | ||
print $my_walk-> | print $my_walk-> | ||
- | $my_walk === $next_walk; // FALSE! | + | $my_walk == $next_walk; // TRUE! |
+ | $my_walk === $next_walk; // TRUE! | ||
</ | </ | ||
- | Tagged Cases may not implement a full constructor. | + | Tagged Cases may not implement a full constructor. |
The Enum Type itself may not define associated values. Only a Case may do so. | The Enum Type itself may not define associated values. Only a Case may do so. | ||
- | |||
- | Associated values are always read-only, both internally to the class and externally. Therefore, making them public does not pose a risk of 3rd party code modifying them inadvertently. They may, however, have attributes associated with them like any other property. | ||
On a Tagged Case enumeration, | On a Tagged Case enumeration, | ||
Line 63: | Line 62: | ||
// This is a Unit Case. | // This is a Unit Case. | ||
case None { | case None { | ||
- | public function bind(callable $f) | + | public function bind(callable $f) |
{ | { | ||
return $this; | return $this; | ||
Line 70: | Line 69: | ||
// This is a Tagged Case. | // This is a Tagged Case. | ||
- | case Some(private | + | case Some(mixed $value) { |
// Note that the return type can be the Enum itself, thus restricting the return | // Note that the return type can be the Enum itself, thus restricting the return | ||
// value to one of the enumerated types. | // value to one of the enumerated types. | ||
Line 79: | Line 78: | ||
} | } | ||
}; | }; | ||
- | |||
- | // This method is available on both None and Some. | ||
- | public function value(): mixed { | ||
- | if ($this instanceof None) { | ||
- | throw new Exception(); | ||
- | } | ||
- | return $this-> | ||
- | } | ||
} | } | ||
</ | </ | ||
Line 104: | Line 95: | ||
enum Command { | enum Command { | ||
- | case Move(public | + | case Move(CardinalDirection $direction, int $distance); |
- | case Turn(public | + | case Turn(Direction $dir); |
case Shoot; | case Shoot; | ||
} | } | ||
</ | </ | ||
+ | === Static analysis type tracking === | ||
+ | |||
+ | PHP's types could be represented with an enum like so: | ||
+ | |||
+ | <PHP> | ||
+ | enum Type | ||
+ | { | ||
+ | case Int; | ||
+ | case Float; | ||
+ | case String; | ||
+ | case Array; | ||
+ | case Bool; | ||
+ | case Object(string $class); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | (I really have had to do something like that before.) | ||
+ | |||
+ | === MongoDB Driver === | ||
+ | |||
+ | The MongoDB team has reached out to us with another example of what they would like to be able to do in their driver. | ||
+ | |||
+ | Their planned code is below, for reference. | ||
+ | |||
+ | <code php> | ||
+ | enum ReadPreference | ||
+ | { | ||
+ | case Primary; | ||
+ | case Secondary(? | ||
+ | case PrimaryPreferred(? | ||
+ | case SecondaryPreferred(? | ||
+ | case Nearest(? | ||
+ | | ||
+ | static function withTagSets(? | ||
+ | static function withMaxStaleness(? | ||
+ | } | ||
+ | |||
+ | enum ReadConcern | ||
+ | { | ||
+ | case Available; | ||
+ | case Linearizable; | ||
+ | case Local; | ||
+ | case Majority; | ||
+ | case Snapshot(? | ||
+ | } | ||
+ | |||
+ | enum WriteConcern | ||
+ | { | ||
+ | case Majority(? | ||
+ | case Unacknowledged; | ||
+ | case Acknowledged(int $servers = 1, ?int $timeout = null); | ||
+ | | ||
+ | static function withTimeout(? | ||
+ | } | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 117: | Line 163: | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | PHP 8.1. | + | PHP 8.3. |
===== Open Issues ===== | ===== Open Issues ===== |
rfc/tagged_unions.1607199665.txt.gz · Last modified: 2020/12/05 20:21 by crell