rfc:enumerations_and_adts

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
Next revisionBoth sides next revision
rfc:enumerations_and_adts [2020/09/19 21:53] crellrfc:enumerations_and_adts [2020/09/19 21:58] crell
Line 184: Line 184:
 Enumerated Cases may optionally include associated values.  An associated value is one that is associated with an instance of a Case.  If a Case has associated values, it will //**not**// be implemented as a singleton.  Each instance of the Case will then be its own object instance, so will not === another instance. Enumerated Cases may optionally include associated values.  An associated value is one that is associated with an instance of a Case.  If a Case has associated values, it will //**not**// be implemented as a singleton.  Each instance of the Case will then be its own object instance, so will not === another instance.
  
-Bound values are defined using constructor property promotion.+Associated values are defined using constructor property promotion.
  
 <code php> <code php>
Line 226: Line 226:
 </code> </code>
  
-That is not true when dealing with Associable Cases.  Therefore, an alternate version of ''%%match%%'' is included.  When ''%%match%%'' is suffixed with ''%%type%%'', it will perform an '"%%instanceof%%'' check instead of an identity check.+That is not true when dealing with Associable Cases.  Therefore, an alternate version of ''%%match%%'' is included.  When ''%%match%%'' is suffixed with ''%%type%%'', it will perform an ''%%instanceof%%'' check instead of an identity check.
  
 <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::None => throw new Exception(),         Optional::None => throw new Exception(),
         Optional::Some => $this->val,         Optional::Some => $this->val,
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:
   };   };
 } }
 +</code>
  
 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.  That means no tests need to be written or code paths defined for going from Off to Idle, because it's literally impossible to even describe that state. 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.  That means no tests need to be written or code paths defined for going from Off to Idle, because it's literally impossible to even describe that state.
rfc/enumerations_and_adts.txt · Last modified: 2020/12/04 23:26 by crell