rfc:compact-object-property-assignment
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
rfc:compact-object-property-assignment [2020/03/18 02:17] – jgivoni | rfc: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 **// | 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 391: | Line 391: | ||
Furthermore, | Furthermore, | ||
+ | |||
+ | However, there might be an opening for a cool syntax: | ||
+ | |||
+ | <code php> | ||
+ | (new Foo){ | ||
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = 3, | ||
+ | } | ||
+ | </ | ||
+ | Since the [[https:// | ||
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 | + | Michał Brzuchalski: |
+ | |||
+ | > This helps to avoid bugs where a property is added to the class but forgot | ||
+ | |||
+ | Mandatory | ||
+ | |||
+ | 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) {...} | ||
+ | } | ||
+ | </ | ||
+ | * Indicate that a property must be initialized before end of constructor: | ||
+ | |||
+ | <code php> | ||
+ | class Foo { | ||
+ | required public string $name; | ||
+ | } | ||
+ | </ | ||
+ | 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 | ||
- | - 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