rfc:compact-object-property-assignment
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:compact-object-property-assignment [2020/03/18 02:17] – jgivoni | rfc:compact-object-property-assignment [2020/03/22 03:17] – jgivoni | ||
---|---|---|---|
Line 3: | Line 3: | ||
**COPA: A // | **COPA: A // | ||
- | * Version: 1.2 | + | * Version: 1.3 |
* Date: 2020-03-17 | * Date: 2020-03-17 | ||
* Author: Jakob Givoni < | * Author: Jakob Givoni < | ||
- | * Status: Under Discussion | + | * Status: Under [[https:// |
- | * Discussion: | + | |
===== Introduction ===== | ===== Introduction ===== | ||
Line 19: | Line 18: | ||
As an alternative to writing a data structure as an associative array, COPA gives the data a **// | As an alternative to writing a data structure as an associative array, COPA gives the data a **// | ||
- | You may find that COPA is best suited when you don’t mind public properties | + | You may find that COPA is best suited when you don’t mind public properties, don’t require a constructor |
==== Example ==== | ==== Example ==== | ||
Line 45: | Line 44: | ||
]); | ]); | ||
</ | </ | ||
+ | COPA is everything that comes after the object expression. You can use COPA on any expression that evaluates to an object. COPA is not tied to object construction, | ||
+ | |||
//See more use cases below - and keep an open mind about the syntax; the options are limited these days :-)// | //See more use cases below - and keep an open mind about the syntax; the options are limited these days :-)// | ||
Line 370: | Line 371: | ||
==== Alternative syntaxes ==== | ==== Alternative syntaxes ==== | ||
- | Some alternative syntaxes for COPA has been suggested, but I’m not convinced they can be implemented without hassle: | + | I’m going to suggest some alternative syntaxes, that we can vote on, provided their feasibility has been vetted by an experienced internals developer: |
+ | |||
+ | === Syntax A === | ||
+ | |||
+ | This is the originally proposed one: | ||
<code php> | <code php> | ||
- | $foo = new Foo()[ | + | $foo->[ |
- | property1 | + | a = 1, |
- | property2 | + | b = 2, |
- | ]; | + | c = (new Foo)->[ |
+ | | ||
+ | | ||
+ | ], | ||
+ | ]; | ||
</ | </ | ||
- | For some reason it’s not possible | + | === Syntax B === |
+ | |||
+ | Since the [[https:// | ||
<code php> | <code php> | ||
- | new Foo()-> | + | $foo{ |
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = (new Foo){ | ||
+ | a = 3, | ||
+ | b = 4, | ||
+ | }, | ||
+ | }; | ||
</ | </ | ||
- | It’s necessary to wrap the instantiation in brackets: | + | === Syntax C === |
+ | |||
+ | No wrapper: | ||
<code php> | <code php> | ||
- | (new Foo)->doSomething(); // Ok | + | $foo-> |
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = (new Foo)-> | ||
+ | a = 3, | ||
+ | b = 4, | ||
+ | ;, | ||
+ | ; | ||
</ | </ | ||
- | Which is why I think it will be necessary in my proposal as well. | + | Nesting becomes awkward - how do we jump out again? |
- | Furthermore, | + | === Syntax D === |
+ | Repeating the array for familiarity: | ||
- | ---- | + | <code php> |
+ | $foo | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | ->b = 4, | ||
+ | ;, | ||
+ | ; | ||
+ | </ | ||
+ | Same issue with nested as previous. We need to find a way to express end of COPA block. | ||
- | //Unless someone can convince me that it’s trivial to implement another syntax that looks even better, my stance is that the people who are going to vote no on this because they don’t find the feature useful are not gonna change their mind if the syntax changes, and the people who find this feature useful will prefer rapid adaptation over solving implementation issues.// | + | === Syntax E === |
+ | Like the original but with normal brackets instead of square ones: | ||
+ | |||
+ | <code php> | ||
+ | $foo->( | ||
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = (new Foo)->( | ||
+ | a = 3, | ||
+ | b = 4, | ||
+ | ), | ||
+ | ); | ||
+ | </ | ||
==== 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 455: | ||
> 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 | + | Michał Brzuchalski: |
+ | |||
+ | > This helps to avoid bugs where a property is added to the class but forgot | ||
+ | |||
+ | 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, | ||
+ | |||
+ | For now you must continue to write your own validation code to be carried out at the appropriate “point | ||
+ | |||
+ | ==== 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 484: | ||
- 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 | ||
- | - A more comprehensive solution to this problem | + | - I prefer a more comprehensive solution to this problem |
- | - A thinner | + | - I prefer a narrower |
- | - This breaks backwards compatibility | + | - 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