rfc:compact-object-property-assignment
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:compact-object-property-assignment [2020/03/10 22:53] – jgivoni | rfc:compact-object-property-assignment [2020/04/02 01:56] – Replaced instance of `=>` with `=` jasny | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Compact Object Property Assignment ====== | ====== PHP RFC: Compact Object Property Assignment ====== | ||
- | | + | |
- | * Date: 2020-03-10 | + | **COPA: A // |
+ | |||
+ | | ||
+ | * Date: 2020-03-17 | ||
* Author: Jakob Givoni < | * Author: Jakob Givoni < | ||
- | * Status: | + | * Status: |
===== Introduction ===== | ===== Introduction ===== | ||
==== Summary ==== | ==== Summary ==== | ||
- | **A pragmatic approach to mimicking object literals.** | ||
This RFC proposes a new, compact syntax to assign values to multiple properties on an object in a single expression. | This RFC proposes a new, compact syntax to assign values to multiple properties on an object in a single expression. | ||
+ | |||
+ | This //**pseudo object literal**// notation, (though not limited to such use) is intended to enable the developer to //**create an object and populating it inline**//, f.ex. directly as an argument in a function call. | ||
+ | |||
+ | As an alternative to writing a data structure as an associative array, COPA gives the data a **// | ||
+ | |||
+ | > COPA does not introduce any new concepts or complexities, | ||
==== Example ==== | ==== Example ==== | ||
+ | |||
+ | Let’s start with an example that demonstrates the essence of COPA. | ||
+ | |||
+ | === Instead of doing this... === | ||
+ | |||
<code php> | <code php> | ||
- | // Current syntax | + | $myObj->a = 1; |
- | $myObj->prop_a | + | $myObj->b = 2; |
- | $myObj->prop_b | + | $myObj->c = 3; |
- | $myObj->prop_c | + | </ |
+ | === You will be able to do this: === | ||
- | // COPA syntax | + | <code php> |
- | $myObj->{ | + | $myObj->[ |
- | | + | |
- | | + | |
- | | + | |
- | }; | + | ]; |
</ | </ | ||
+ | > 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.// | ||
==== Motivation ==== | ==== Motivation ==== | ||
- | The idea behind this feature is to lighten the effort of populating medium to large data structures. | ||
- | What I want to achieve | + | The purpose of this feature |
+ | |||
+ | The goal was to find a solution that meets the following criteria: | ||
* **Brief** - only mention the object once (less repetition) | * **Brief** - only mention the object once (less repetition) | ||
* **Inline** - object can be created and populated in a single expression (pseudo object literals, nested objects) | * **Inline** - object can be created and populated in a single expression (pseudo object literals, nested objects) | ||
* **Typo-proof** - property names can be autocompleted easily by IDE (faster typing, fewer errors) | * **Typo-proof** - property names can be autocompleted easily by IDE (faster typing, fewer errors) | ||
- | * **Type-hint enabled** - IDE can verify correct type for typed properties and annotated virtual properties | + | * **Type-checking** - IDE can verify correct type for typed properties and annotated virtual properties |
- | * Order-agnostic - properties can be specified in any order (this doesn' | + | |
- | * No surprises | + | * **Sparcity** - any property can be “skipped” (“skipped” properties may acquire a default value) |
+ | * **Simple** | ||
+ | |||
+ | > My focus is to find a **pragmatic** solution that is trivial to implement, and won’t impede futher development of the language. | ||
+ | |||
+ | //If you have ever wanted to create, populate and send an object inside a function call, COPA is for you!// | ||
===== Proposal ===== | ===== Proposal ===== | ||
==== Syntax ==== | ==== Syntax ==== | ||
- | The proposed syntax | + | |
- | A trailing comma is permitted for the same reasons it's permitted in array literals and function calls (as of PHP 7.3). | + | The proposed syntax |
- | The whole block is considered an expression that returns the object we started | + | |
+ | < | ||
+ | < | ||
+ | `< | ||
+ | | ||
+ | ] | ||
+ | </ | ||
+ | > A trailing comma is permitted for the same reasons it's permitted in array literals and [[https:// | ||
+ | |||
+ | The whole block is considered an expression that returns the object | ||
+ | |||
+ | This syntax was chosen for its availability in the language. If we land on another syntax, I’m not married to this one. The only criteria are that it doesn’t conflict | ||
==== Interpretation ==== | ==== Interpretation ==== | ||
- | Each comma-separated assignment inside the curly brackets is executed as an assignment of the named property on the object preceding the block. If the property is defined and publicly accessible, it will simply be set, or possible throw a TypeError. If there' | ||
- | When used in an expression, COPA simply returns the object itself. | ||
+ | Each comma-separated assignment inside the brackets is executed as an assignment of the named property on the object preceding the block. If the property is defined and publicly accessible, it will simply be set, or possible throw a '' | ||
+ | |||
+ | If you replace COPA with single line assignments, | ||
+ | |||
+ | <code php> | ||
+ | $foo->[ | ||
+ | a = 1, | ||
+ | b = myfunc(), | ||
+ | c = $foo-> | ||
+ | ]; | ||
+ | |||
+ | // The COPA above is identical to | ||
+ | $foo->a = 1; | ||
+ | $foo->b = myfunc(); | ||
+ | $foo->c = $foo-> | ||
+ | </ | ||
==== Use cases ==== | ==== Use cases ==== | ||
+ | === Create and send struct === | ||
+ | |||
+ | <code php> | ||
+ | // Instead of this: | ||
+ | |||
+ | $myObj = new Foo; // 1. Create struct-like object without constructor arguments | ||
+ | |||
+ | $myObj-> | ||
+ | $myObj-> | ||
+ | $myObj-> | ||
+ | |||
+ | doTheFoo($myObj); | ||
+ | |||
+ | // Use COPA: | ||
+ | |||
+ | doTheFoo((new Foo)->[ | ||
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = 3, | ||
+ | ]); | ||
+ | </ | ||
+ | //No boilerplate needed.// | ||
+ | |||
+ | === Stop using arrays === | ||
+ | |||
+ | <code php> | ||
+ | // Instead of this: | ||
+ | |||
+ | doSomething([ | ||
+ | ' | ||
+ | ' | ||
+ | ]); | ||
+ | |||
+ | // Use COPA: | ||
+ | |||
+ | class Options { // Give the data a signature, a well-defined structure | ||
+ | public $a; | ||
+ | public $b; | ||
+ | } | ||
+ | |||
+ | doSomething((new Options)-> | ||
+ | a = 1, // Parameter name and type checking | ||
+ | b = 2, | ||
+ | ]); | ||
+ | </ | ||
+ | //If you often create, populate and send the same families of data structure, declaring those structures and using COPA makes it a breeze.// | ||
+ | |||
+ | === Nested COPA === | ||
+ | |||
+ | COPA is not limited to a flat structure. | ||
+ | |||
+ | <code php> | ||
+ | (new Foo)->[ | ||
+ | om = ' | ||
+ | mane = ' | ||
+ | hum = (new Foo)->[ | ||
+ | mane = ' | ||
+ | ], | ||
+ | ]; | ||
+ | </ | ||
+ | === Split options from services === | ||
+ | |||
+ | Separate concerns and use composition. In this example, once you have instantiated Foo, the options are no longer writeable, even though the options were public properties. | ||
+ | |||
+ | <code php> | ||
+ | class FooOptions { | ||
+ | public ?string $mane = null; | ||
+ | public int $padme = 1; // Optional, with default | ||
+ | public ?string $hum = null; | ||
+ | } | ||
+ | |||
+ | class Foo { | ||
+ | protected FooOptions $options; | ||
+ | |||
+ | public function __construct(FooOptions $options) { | ||
+ | // Do some validate here if you must, f.ex. checking for mandatory parameters | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $myFoo = new Foo((new FooOptions)-> | ||
+ | mane = ' | ||
+ | hum = ' | ||
+ | ]); | ||
+ | </ | ||
+ | //If you can’t wait for “named parameters” and often resort to “parameter bags” this is a perfectly valid and saner alternative.// | ||
+ | |||
+ | ==== Special cases ==== | ||
+ | |||
+ | Clarification of edge-case behavior. | ||
+ | |||
+ | === Execution order === | ||
+ | |||
+ | The fact that the assignments are executed in the order they are listed (just as if they had been specified on separate lines), has the following consequence: | ||
+ | |||
+ | <code php> | ||
+ | $myObj-> | ||
+ | foo = 10, | ||
+ | bar = $myObj-> | ||
+ | ]; | ||
+ | |||
+ | var_dump($myObj-> | ||
+ | </ | ||
+ | > As the assignments are carried out in order on the object, you can use the new value of a previous assigment in a following one. | ||
+ | |||
+ | === Exceptions === | ||
+ | |||
+ | If an expression inside a COPA block throws an exception, the result is the same as if the assignments had been done the old way, f.ex. if we have: | ||
+ | |||
+ | <code php> | ||
+ | class Foo { | ||
+ | public $a; | ||
+ | public $b; | ||
+ | public $c; | ||
+ | } | ||
+ | |||
+ | $foo = new Foo(); | ||
+ | |||
+ | function iThrow() { | ||
+ | throw new \Exception(); | ||
+ | } | ||
+ | </ | ||
+ | Then the following examples behave identically: | ||
+ | |||
+ | <code php> | ||
+ | // With COPA: | ||
+ | |||
+ | try { | ||
+ | $foo->[ | ||
+ | a = ' | ||
+ | b = iThrow(), | ||
+ | c = ' | ||
+ | ]; | ||
+ | } catch (\Throwable $e) { | ||
+ | var_dump($foo); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | <code php> | ||
+ | // Without COPA: | ||
+ | |||
+ | try { | ||
+ | $foo-> | ||
+ | -> | ||
+ | -> | ||
+ | } catch (\Throwable $e) { | ||
+ | var_dump($foo); | ||
+ | } | ||
+ | |||
+ | // OR | ||
+ | |||
+ | try { | ||
+ | $foo->a = ' | ||
+ | $foo->b = iThrow(); | ||
+ | $foo->c = ' | ||
+ | } catch (\Throwable $e) { | ||
+ | var_dump($foo); | ||
+ | } | ||
+ | </ | ||
+ | The result in all cases is that '' | ||
+ | |||
+ | < | ||
+ | object(Foo)# | ||
+ | [" | ||
+ | string(1) " | ||
+ | [" | ||
+ | NULL | ||
+ | [" | ||
+ | NULL | ||
+ | } | ||
+ | </ | ||
+ | > COPA is **not** an atomic operation in the same way that method chaining isn’t. | ||
+ | |||
+ | ==== Out of scope / future scope ==== | ||
+ | |||
+ | This section contains features that is not considered for implementation in version 1 of COPA but may be considered later. | ||
+ | |||
+ | === You can’t do that === | ||
+ | |||
+ | The following examples show various things that are currently possible when using regular property accessor, though they won’t work inside a COPA block: | ||
+ | |||
+ | <code php> | ||
+ | $p = ' | ||
+ | $myObj-> | ||
+ | $a-> | ||
+ | $a-> | ||
+ | $a-> | ||
+ | $a->f++; // Increment/ | ||
+ | |||
+ | $myObj-> | ||
+ | $p = ' | ||
+ | {" | ||
+ | b->c = ' | ||
+ | d[' | ||
+ | f++, // Syntax error | ||
+ | ]; | ||
+ | </ | ||
+ | //These can be implemented in the future if there is a demand.// | ||
+ | |||
+ | === Nested COPA on existing objects === | ||
+ | |||
+ | The following syntax could be supported in the future: | ||
+ | |||
+ | <code php> | ||
+ | // This example, using current syntax... | ||
+ | |||
+ | $foo->a = 1; | ||
+ | $foo-> | ||
+ | |||
+ | // Could be written with COPA like this: | ||
+ | |||
+ | $foo->[ | ||
+ | a = 1, | ||
+ | b->[ | ||
+ | c = 2, | ||
+ | ], | ||
+ | ]; | ||
+ | |||
+ | // But for now you'll have to do this: | ||
+ | |||
+ | $foo->[ | ||
+ | a = 1, | ||
+ | b = $foo-> | ||
+ | c = 2, | ||
+ | ], | ||
+ | ]; | ||
+ | |||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
+ | |||
+ | None. | ||
+ | |||
+ | > **Note!** Array followed by square bracket causes syntax error in PHP 7.4. This new syntax is optional. If you don't use it, your code will continue to run. | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | Next PHP 8.x | + | |
+ | PHP 8.0 | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | Make sure there are no open issues when the vote starts! | ||
- | ===== Proposed Voting Choices | + | ==== Alternative syntaxes |
- | Include these so readers know where you are heading | + | |
+ | I’m going to suggest some alternative syntaxes, which 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> | ||
+ | $foo-> | ||
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = (new Foo)-> | ||
+ | a = 3, | ||
+ | b = 4, | ||
+ | ], | ||
+ | ]; | ||
+ | </ | ||
+ | === Syntax B === | ||
+ | |||
+ | Since the [[https:// | ||
+ | |||
+ | <code php> | ||
+ | $foo{ | ||
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = (new Foo){ | ||
+ | a = 3, | ||
+ | b = 4, | ||
+ | }, | ||
+ | }; | ||
+ | </ | ||
+ | > Going from deprecation in 7.4 to removal of support in 8.0 may is not unprecedented. Old code that has not been mended won’t silently do something spurious. | ||
+ | |||
+ | === Syntax C === | ||
+ | |||
+ | No wrapper: | ||
+ | |||
+ | <code php> | ||
+ | $foo-> | ||
+ | a = 1, | ||
+ | b = 2, | ||
+ | c = (new Foo)-> | ||
+ | a = 3, | ||
+ | b = 4, | ||
+ | ;, | ||
+ | ; | ||
+ | </ | ||
+ | Nesting becomes awkward - how do we jump out again? | ||
+ | |||
+ | > **Note!** This looks more like a chain of normal assignments, | ||
+ | |||
+ | === Syntax D === | ||
+ | |||
+ | Repeating the arrow for familiarity with regular property assignment: | ||
+ | |||
+ | <code php> | ||
+ | $foo | ||
+ | ->a = 1, | ||
+ | ->b = 2, | ||
+ | ->c = (new Foo) | ||
+ | ->a = 3, | ||
+ | ->b = 4, | ||
+ | ;, | ||
+ | ; | ||
+ | </ | ||
+ | Same issues as previous. | ||
+ | |||
+ | === 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, | ||
+ | ), | ||
+ | ); | ||
+ | </ | ||
+ | === Syntax F === | ||
+ | |||
+ | **WITH** syntax | ||
+ | |||
+ | <code php> | ||
+ | myObj.with { | ||
+ | foo = 10 | ||
+ | bar = foo + 20 | ||
+ | } | ||
+ | </ | ||
+ | If this is the preferred syntax it will require a new RFC. | ||
+ | |||
+ | ===== Rejected Features ===== | ||
+ | |||
+ | Some suggested features have been rejected due to the fact that COPA aims to be pragmatic, with a trivial implementation and without introducing any new concepts to avoid a combinatorial explosion of complexities in the future. | ||
+ | |||
+ | ==== Mandatory properties ==== | ||
+ | |||
+ | Some have voiced criticism that COPA is of little use without also enforcing mandatory properties to be set. | ||
+ | |||
+ | **Rowan Tommins: | ||
+ | |||
+ | > It seems pretty rare that an object would have no mandatory properties, | ||
+ | |||
+ | **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 | ||
+ | |||
+ | > COPA won’t support this since COPA doesn’t introduce any new concepts or complexities. The lack of this feature is not a limitation of COPA when compared to current functionality. | ||
+ | |||
+ | //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 (i.e. like a “transaction”). | ||
+ | |||
+ | Though that sounds cool, this is an edge case that won’t have any significant impact. If you were planning to resume gracefully with an incomplete object you should probably reconsider your goals in life. | ||
+ | |||
+ | > **Note!** Chaining method calls is not an atomic operation either. The cost/ | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | Voting starts 2020-03-31 and ends 2020-04-13. | ||
+ | |||
+ | The primary vote of whether or not to accept this RFC requires a 2/3 majority. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | <doodle title=" | ||
+ | * I voted yes! | ||
+ | * I don’t find the feature useful | ||
+ | * I don’t like the syntax | ||
+ | * I prefer a more comprehensive solution to this problem | ||
+ | * I prefer a narrower solution to this problem | ||
+ | * This breaks backwards compatibility | ||
+ | * This will have negative implications for future language evolution | ||
+ | * This will be a nightmare to implement and maintain | ||
+ | * I prefer not to say | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | <doodle title=" | ||
+ | * A (the proposed one) | ||
+ | * B | ||
+ | * C | ||
+ | * D | ||
+ | * E | ||
+ | * F | ||
+ | * Irrelevant | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
+ | |||
+ | There are yet no patches nor tests. The question of who will be developing this will be addressed if the RFC passes. | ||
===== Implementation ===== | ===== Implementation ===== | ||
- | After the project is implemented, | + | |
+ | After the project is implemented, | ||
- the version(s) it was merged into | - the version(s) it was merged into | ||
- a link to the git commit(s) | - a link to the git commit(s) | ||
Line 74: | Line 516: | ||
===== References ===== | ===== References ===== | ||
- | ===== Rejected Features ===== | + | Related RFCs: |
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
rfc/compact-object-property-assignment.txt · Last modified: 2020/04/14 06:30 by jgivoni