rfc:enumerations_and_adts
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:enumerations_and_adts [2020/09/19 21:53] – crell | rfc:enumerations_and_adts [2020/09/19 21:58] – crell | ||
---|---|---|---|
Line 184: | Line 184: | ||
Enumerated Cases may optionally include associated values. | Enumerated Cases may optionally include associated values. | ||
- | Bound values are defined using constructor property promotion. | + | Associated |
<code php> | <code php> | ||
Line 226: | Line 226: | ||
</ | </ | ||
- | That is not true when dealing with Associable Cases. | + | That is not true when dealing with Associable Cases. |
<code php> | <code php> | ||
Line 257: | Line 257: | ||
| | ||
// This is an Associable Case. | // This is an Associable Case. | ||
- | case Some { | + | case Some(private 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 269: | Line 269: | ||
public function value(): mixed { | public function value(): mixed { | ||
// Still need to sort out match() for this to make sense. | // Still need to sort out match() for this to make sense. | ||
- | return match enum ($this) { | + | return match type ($this) { |
Optional:: | Optional:: | ||
Optional:: | Optional:: | ||
Line 281: | Line 281: | ||
Enums make it straightforward to express finite state machines. | Enums make it straightforward to express finite state machines. | ||
+ | <code php> | ||
enum OvenStatus { | enum OvenStatus { | ||
Line 296: | Line 297: | ||
}; | }; | ||
} | } | ||
+ | </ | ||
In this example, the oven can be in one of three states (Off, On, and Idling, meaning the flame is not on but it will turn back on when it detects it needs to). However, it can never go from Off to Idle or Idle to Off; it must go through On state first. | In this example, the oven can be in one of three states (Off, On, and Idling, meaning the flame is not on but it will turn back on when it detects it needs to). However, it can never go from Off to Idle or Idle to Off; it must go through On state first. |
rfc/enumerations_and_adts.txt · Last modified: 2020/12/04 23:26 by crell