rfc:structs-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:structs-v2 [2024/04/23 16:32] ilutovrfc:structs-v2 [2024/04/24 20:33] (current) ilutov
Line 276: Line 276:
  
 Only mutating methods can and must be called using the ''!()'' syntax. Calling mutating methods with ''()'', or non-mutating methods with ''!()'' results in a runtime error. Only mutating methods can and must be called using the ''!()'' syntax. Calling mutating methods with ''()'', or non-mutating methods with ''!()'' results in a runtime error.
 +
 +Similarly, classes trying to implement ''mutating'' methods will compile error.
  
 TOOD: Check if we can enforce ''mutating'' at compile-time, anytime ''$this'' is fetched with RW (assignments, calling of mutating methods, fetching references). TOOD: Check if we can enforce ''mutating'' at compile-time, anytime ''$this'' is fetched with RW (assignments, calling of mutating methods, fetching references).
Line 367: Line 369:
 Inheritance is currently not allowed for structs. Structs are mainly targeted at data modelling, which should prefer composition over inheritance. There are currently no known technical issues with inheritance for structs, but we may want to be cautious when introducing them, and carefully consider the plethora of subtle semantic nuances. Inheritance is currently not allowed for structs. Structs are mainly targeted at data modelling, which should prefer composition over inheritance. There are currently no known technical issues with inheritance for structs, but we may want to be cautious when introducing them, and carefully consider the plethora of subtle semantic nuances.
  
-Implementing interfaces is allowed, however.+Implementing interfaces is allowed, however. Interface methods may be ''mutating'', which will be enforced when implementing the interface method. However, they may obviously only be implemented by structs, but not classes.
  
 ===== Hashing ===== ===== Hashing =====
Line 395: Line 397:
     $moved = $value;     $moved = $value;
     $value = null;     $value = null;
-    return $value;+    return $moved;
 } }
  
Line 408: Line 410:
 ===== Performance ===== ===== Performance =====
  
-Because ''->'' needs to clone the object if the value will be mutatedan additional check was added to various code paths. In my benchmarks, this lead to a small performance of +0.07%, whether you use structs or not. The benchmark was performed on Symfony Demo, with Opcache.+Assignment to a property now needs to check whether the object is a struct objectand then clone it. This change was necessary in various code paths. In my benchmarks, this lead to a small slowdown of +0.07%, whether you use structs or not. The benchmark was performed on Symfony Demo, with Opcache.
  
 ===== Backwards incompatible changes ===== ===== Backwards incompatible changes =====
  
-''struct'' becomes a keyword in this RFC. There are no other backwards incompatible changes.+''struct'' needs to become a keyword in this RFC. However, ''struct'' will only be considered a keyword when it is followed by another identifier, excluding ''extends'' and ''implements''. This is the same approach used for the [[https://wiki.php.net/rfc/enumerations#backward_incompatible_changes|enum RFC]], and thus completely avoided backwards incompatible changes. 
 + 
 +There are no other backwards incompatible changes.
  
 ===== Vote ===== ===== Vote =====
rfc/structs-v2.1713889947.txt.gz · Last modified: 2024/04/23 16:32 by ilutov