rfc:compact-object-property-assignment

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:compact-object-property-assignment [2020/03/18 02:17] jgivonirfc:compact-object-property-assignment [2020/03/19 02:35] jgivoni
Line 19: Line 19:
 As an alternative to writing a data structure as an associative array, COPA gives the data a **//documented signature//** so that you know what parameters are expected and their values. As an alternative to writing a data structure as an associative array, COPA gives the data a **//documented signature//** so that you know what parameters are expected and their values.
  
-You may find that COPA is best suited when you don’t mind public properties you don’t require a constructor, since as [[https://wiki.php.net/rfc/write_once_properties#read-only_semantics|the saying goes]]: //Object construction is a fuzzy concept in PHP and lazy initialisation is a feature!//+You may find that COPA is best suited when you don’t mind public propertiesdon’t require a constructor and is willing to compromise on magic enforcement of mandatory properties, since as [[https://wiki.php.net/rfc/write_once_properties#read-only_semantics|the saying goes]]: //Object construction is a fuzzy concept in PHP and lazy initialisation is a feature!//
  
 ==== Example ==== ==== Example ====
Line 391: Line 391:
  
 Furthermore, a variable or object directly followed by square brackets usually imply array access on it. That syntax would conflict with COPA. Furthermore, a variable or object directly followed by square brackets usually imply array access on it. That syntax would conflict with COPA.
 +
 +However, there might be an opening for a cool syntax:
 +
 +<code php>
 +(new Foo){
 +    a = 1,
 +    b = 2,
 +    c = 3,
 +}
 +</code>
 +Since the [[https://wiki.php.net/rfc/deprecate_curly_braces_array_access|deprecation of curly brackets as array access in PHP 7.4]], that notation could be used to assign properties.
  
  
Line 398: Line 409:
  
 ==== Mandatory properties ==== ==== Mandatory properties ====
 +
 +Some criticism has been that COPA is of little use without also enforcing mandatory properties to be set.
  
 Rowan Tommins: Rowan Tommins:
Line 403: Line 416:
 > It seems pretty rare that an object would have no mandatory properties, so saying “if you have a mandatory property, COPA is not for you” is ruling out a lot of uses. > It seems pretty rare that an object would have no mandatory properties, so saying “if you have a mandatory property, COPA is not for you” is ruling out a lot of uses.
  
-Is it possible to amend the proposal to help with mandatory properties? Or at least give an example of how you would add the check yourself?+Michał Brzuchalski: 
 + 
 +> This helps to avoid bugs where a property is added to the class but forgot to be assigned it a value in all cases where the class is instantiated and initialized 
 + 
 +Mandatory properties are typed properties without a default value. They are in the uninitialized state until they are assigned a value. It has been suggested that an exception should be thrown at the end of the constructor if any property is still uninitialized, but this idea has not yet caught on. 
 + 
 +I have a few ideas of how to make progress with that, but it’s not part of COPA itself: 
 + 
 +  * Indicate that a parameter is required to be fully initialized when accepting into a function, f.ex: 
 + 
 +<code php> 
 +class Bar { 
 +    public function doTheFoo(!Foo $foo) {...} 
 +
 +</code> 
 +  * Indicate that a property must be initialized before end of constructor: 
 + 
 +<code php> 
 +class Foo { 
 +    required public string $name; 
 +
 +</code> 
 +But for now you must continue to write your own validation code to be carried out at the appropriate “point of no return”. 
 + 
 +==== Atomic operations ==== 
 + 
 +It’s also been suggested that assigning multiple values using COPA should be an atomic operation that either succeeds or fails in its entirety. 
 + 
 +That does sound cool as well, and may seem like the expected behavior for some. 
 + 
 +Still, I’m not convinved it’s worth the extra hassle, since what were you planning to do with the incomplete object anyway? 
 + 
 +Chaining method calls are not an atomic operation and if an exception is thrown in the middle I doubt you would raise an eyebrow about the previous call having altered the state of the object.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
Line 416: Line 461:
   - I don’t find the feature useful   - I don’t find the feature useful
   - I don’t like the syntax   - I don’t like the syntax
-  - more comprehensive solution to this problem will be better +  - I prefer a more comprehensive solution to this problem 
-  - A thinner solution to this problem will be better +  - I prefer a narrower solution to this problem 
-  - This breaks backwards compatibility too much+  - This breaks backwards compatibility
   - This will negatively limit future changes   - This will negatively limit future changes
-  - It will be a nightmare to implement and maintain+  - This will be a nightmare to implement and maintain
   - I prefer not to say   - I prefer not to say
  
rfc/compact-object-property-assignment.txt · Last modified: 2020/04/14 06:30 by jgivoni