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/19 02:35] – 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 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: |
- | <code php> | + | === Syntax A === |
- | $foo = new Foo()[ | + | |
- | | + | This is the originally proposed one: |
- | | + | |
- | ]; | + | |
- | </ | + | |
- | For some reason it’s not possible to do this: | + | |
<code php> | <code php> | ||
- | new Foo()->doSomething(); // syntax error, unexpected '->' | + | $foo->[ |
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = (new Foo)->[ | ||
+ | a = 3, | ||
+ | b = 4, | ||
+ | ], | ||
+ | ]; | ||
</ | </ | ||
- | It’s necessary to wrap the instantiation in brackets: | + | === Syntax B === |
+ | |||
+ | Since the [[https:// | ||
<code php> | <code php> | ||
- | (new Foo)-> | + | $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. | + | === Syntax C === |
- | + | ||
- | Furthermore, | + | |
- | However, there might be an opening for a cool syntax: | + | No wrapper: |
<code php> | <code php> | ||
- | (new Foo){ | + | $foo-> |
a = 1, | a = 1, | ||
b = 2, | b = 2, | ||
- | c = 3, | + | c = (new Foo)-> |
- | } | + | a = 3, |
+ | b = 4, | ||
+ | ;, | ||
+ | ; | ||
</ | </ | ||
- | Since the [[https:// | + | Nesting becomes awkward - how do we jump out again? |
+ | === Syntax D === | ||
- | ---- | + | Repeating the array for familiarity: |
- | //Unless someone can convince me that it’s trivial | + | <code php> |
+ | $foo | ||
+ | ->a = 1, | ||
+ | ->b = 2, | ||
+ | ->c = (new Foo) | ||
+ | ->a = 3, | ||
+ | ->b = 4, | ||
+ | ;, | ||
+ | ; | ||
+ | </code> | ||
+ | Same issue with nested as previous. We need to find a way to express end of COPA block. | ||
+ | === 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 ==== | ||
Line 420: | Line 459: | ||
> 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 | > 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, | + | 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, |
- | I have a few ideas of how to make progress with that, but it’s not part of COPA itself: | + | For now you must continue to write your own validation code to be carried out at the appropriate “point of no return”. |
- | + | ||
- | * 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 ==== | ==== Atomic operations ==== |
rfc/compact-object-property-assignment.txt · Last modified: 2020/04/14 06:30 by jgivoni