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:26] 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 337: Line 339:
 TODO: This is actually broken currently. TODO: This is actually broken currently.
  
-This modification is //not// considered mutating, because the object may change from some other place anyway. Structs behave closer to objects, so interior mutation is not allowed.+This modification is //not// considered mutating, because the object may change from some other place anyway. Structs behave closer to arrays, so interior mutation is not allowed.
  
 <code php> <code php>
Line 350: Line 352:
 <code php> <code php>
 $bigNum1 = new BigNum(1); $bigNum1 = new BigNum(1);
-$bigNum2 = new BigNum(1);+$bigNum2 = $bigNum1;
  
 $reflection = new ReflectionProperty(BigNum::class, 'value'); $reflection = new ReflectionProperty(BigNum::class, 'value');
-$reflection->setValue($bigNum, 2);+$reflection->setValue($bigNum2, 2); 
 + 
 +// Desired behavior 
 +var_dump($bigNum1, $bigNum2); // 1, 2
 </code> </code>
  
-To work properly, ''ReflectionProperty::setValue()'' would need to accept a reference for the ''$objectOrValue'' property. This change would break existing cases where ''$objectOrValue'' is a temporary value (e.g. the result of a function call). There's also the special ''@prefer-ref'' annotation that is only available for internal functions. If the value can be passed by reference, it is. Otherwise, it is passed by value. This solution works well, but breaks userland overrides of ''ReflectionProperty::setValue()'' with no possibility of mitigation, because ''@prefer-ref'' is not available in userland.+for this to work properly, ''ReflectionProperty::setValue()'' would need to accept a reference for the ''$objectOrValue'' property. That is because internal functions are assumed not to mutate struct objects when they are accepted by value, because the copy could not be written back to the original variable. Making ''$objectOrValue'' by-reference would break existing code where ''$objectOrValue'' is a temporary value (e.g. the result of a function call). There's also the special ''@prefer-ref'' annotation that is only available for internal functions. If the value can be passed by reference, it is. Otherwise, it is passed by value. This solution works well, but breaks userland overrides of ''ReflectionProperty::setValue()'' with no possibility of mitigation, because ''@prefer-ref'' is not available in userland.
  
 For this reason, I have opted to throw when passing a struct object to ''ReflectionProperty::setValue()'' for the time being. For this reason, I have opted to throw when passing a struct object to ''ReflectionProperty::setValue()'' for the time being.
Line 363: Line 368:
  
 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. 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 390: Line 397:
     $moved = $value;     $moved = $value;
     $value = null;     $value = null;
-    return $value;+    return $moved;
 } }
  
Line 403: 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.1713889607.txt.gz · Last modified: 2024/04/23 16:26 by ilutov