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/19 02:35] jgivonirfc:compact-object-property-assignment [2020/03/22 03:17] jgivoni
Line 3: Line 3:
 **COPA: A //pragmatic// approach to object literals** **COPA: A //pragmatic// approach to object literals**
  
-  * Version: 1.2+  * Version: 1.3
   * Date: 2020-03-17   * Date: 2020-03-17
   * Author: Jakob Givoni <jakob@givoni.dk>   * Author: Jakob Givoni <jakob@givoni.dk>
-  * Status: Under Discussion +  * Status: Under [[https://externals.io/message/109055|Discussion]]
-  * Discussion: https:%%//%%externals.io/message/109055+
  
 ===== Introduction ===== ===== Introduction =====
Line 45: Line 44:
 ]); ]);
 </code> </code>
 +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, but can be used anytime, anywhere in the objects life, as many times as you want.
 +
 //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()[ + 
-   property1 "hello", +This is the originally proposed one:
-   property2 5, +
- ]; +
-</code> +
-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, 
 +    ], 
 +];
 </code> </code>
-It’s necessary to wrap the instantiation in brackets:+=== Syntax B === 
 + 
 +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:
  
 <code php> <code php>
-(new Foo)->doSomething()// Ok+$foo{ 
 +    a = 1, 
 +    b = 2, 
 +    c = (new Foo)
 +        a = 3, 
 +        b = 4, 
 +    }, 
 +};
 </code> </code>
-Which is why I think it will be necessary in my proposal as well. +=== Syntax C ===
- +
-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:+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, 
 +    ;, 
 +;
 </code> </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.+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 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.//+<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,
 +    ),
 +);
 +</code>
 ==== 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, but this idea has not yet caught on.+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. COPA doesn’t have any obvious way of enforcing mandatory properties.
  
-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) {...} +
-+
-</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 ==== ==== Atomic operations ====
rfc/compact-object-property-assignment.txt · Last modified: 2020/04/14 06:30 by jgivoni