Re: [PHP-DEV] [RFC] [DISCUSSION] Compact Object Property Assignment

This is only part of a thread. view whole thread
  109091
March 17, 2020 02:07 jakob@givoni.dk (Jakob Givoni)
On Mon, Mar 16, 2020 at 6:48 AM Jakob Givoni <jakob@givoni.dk> wrote:
> Let me know what you think!
Thank you for your feedback so far! I'd be really curios to know what authors of the referenced RFCs think about this, f.ex. Michał (brzuchal@php.net), Andrey (andrewgrom@rambler.ru) and Nikita (nikic@php.net) Is simple inline object population useful in itself? Is the proposed implementation as trivial as I think it is? Best regards, Jakob
  109106
March 17, 2020 15:52 michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=)
Hi Jakob,

wt., 17 mar 2020 o 03:08 Jakob Givoni <jakob@givoni.dk> napisał(a):

> On Mon, Mar 16, 2020 at 6:48 AM Jakob Givoni <jakob@givoni.dk> wrote: > > Let me know what you think! > > Thank you for your feedback so far! > > I'd be really curios to know what authors of the referenced RFCs think > about this, f.ex. > Michał (brzuchal@php.net), Andrey (andrewgrom@rambler.ru) and Nikita > (nikic@php.net) > > Is simple inline object population useful in itself? > Is the proposed implementation as trivial as I think it is? >
For object initializer, I was hoping to introduce a feature which with the benefits of typed properties could reduce the boilerplate on initializing object and setting their properties in one expression. This is somewhat what I personally do a lot for commands and events construction where those simply have public typed properties and I don't care if something would be changed in the meantime. With current RFC for write-once, I can see object initializer could be an awesome feature with a combine of read-only properties. Currently I would have to do a lot of: $command = new SomeCommand(); $command->foo = 'bar'; $command->baz = true $this->dispatch($command); For SomeCommand looking like (with read-only for example purposes) class SomeCommand { public readonly string $foo; public readonly bool $baz; } With object initializer, this could be reduced to: $this->dispatch(new SomeCommand { foo = 'bar', baz = false }); But as mentioned IMO this is a different feature than what you propose and personally I see no point in reducing only assignment statements without combining it with object construction for these kinds of small objects, DTO's, commands, queries and events. And agree with others that brackets look unnatural for anything related to objects. IIRC there were many questions about named parameters to pass for object constructor or proper property setters like in C#, but personally I don't need a constructor at all in those cases since we have typed properties and it looks like they could be also marked as read-only in next major PHP version. Cheers, -- Michał Brzuchalski
  109118
March 18, 2020 02:36 jakob@givoni.dk (Jakob Givoni)
Thank you, Michał, for chiming in :-)

On Tue, Mar 17, 2020 at 10:52 AM Michał Brzuchalski
brzuchalski@gmail.com> wrote:
> For object initializer, I was hoping to introduce a feature which with the benefits of typed properties > could reduce the boilerplate on initializing object and setting their properties in one expression. Exactly my motivation with COPA as well;
- Typed properties - check, - Reduce boilerplate - check, - Initializing and setting properties in one expression - check.
> With object initializer, this could be reduced to: > > $this->dispatch(new SomeCommand { foo = 'bar', baz = false }); And with COPA it would be;
$this->dispatch((new SomeCommand)->[ foo = 'bar', baz = false ]); Subtle differences, when compared to the currently available alternative.
> But as mentioned IMO this is a different feature than what you propose and personally I see no point > in reducing only assignment statements without combining it with object construction for these kinds > of small objects, DTO's, commands, queries and events. Maybe you missed that COPA can be combined with object construction?
> but personally I don't need a constructor at all in those cases since we > have typed properties and it looks like they could be also marked as read-only in next major PHP version. Exactly, and as I couldn't help notice that you voted for the Write
Once Properties RFC, which clearly states that object construction is a fuzzy term (meaning don't take it too seriously) and lazy initialization is a feature... I hope you will either reconsider your support for COPA or let me know what I'm missing so I can consider other directions.
  109122
March 18, 2020 05:27 michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=)
śr., 18 mar 2020, 03:36 użytkownik Jakob Givoni <jakob@givoni.dk> napisał:

> Thank you, Michał, for chiming in :-) > > On Tue, Mar 17, 2020 at 10:52 AM Michał Brzuchalski > brzuchalski@gmail.com> wrote: > > For object initializer, I was hoping to introduce a feature which with > the benefits of typed properties > > could reduce the boilerplate on initializing object and setting their > properties in one expression. > Exactly my motivation with COPA as well; > - Typed properties - check, > - Reduce boilerplate - check, > - Initializing and setting properties in one expression - check. > > > With object initializer, this could be reduced to: > > > > $this->dispatch(new SomeCommand { foo = 'bar', baz = false }); > And with COPA it would be; > $this->dispatch((new SomeCommand)->[ foo = 'bar', baz = false ]); > Subtle differences, when compared to the currently available alternative. >
The difference is in object state initialization : Using Object Initializer enforce that if a class is instantiated with the Object Initializer, at the end of the instantiation and properties initialization, all visible properties *(depends on initialization scope)* are initialized, otherwise, a RuntimeException is thrown. 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. https://wiki.php.net/rfc/object-initializer#restrictions You can't do that with COPA cause it has nothing to do with object instantiation.
> > But as mentioned IMO this is a different feature than what you propose > and personally I see no point > > in reducing only assignment statements without combining it with object > construction for these kinds > > of small objects, DTO's, commands, queries and events. > Maybe you missed that COPA can be combined with object construction? > > > but personally I don't need a constructor at all in those cases since we > > have typed properties and it looks like they could be also marked as > read-only in next major PHP version. > Exactly, and as I couldn't help notice that you voted for the Write > Once Properties RFC, which clearly states that > object construction is a fuzzy term (meaning don't take it too > seriously) and lazy initialization is a feature... > > I hope you will either reconsider your support for COPA or let me know > what I'm missing so I can > consider other directions. >
  109175
March 20, 2020 23:47 jakob@givoni.dk (Jakob Givoni)
On Wed, Mar 18, 2020 at 12:27 AM Michał Brzuchalski
brzuchalski@gmail.com> wrote:

> Using Object Initializer enforce that if a class is instantiated with the Object Initializer, > at the end of the instantiation and properties initialization, all visible properties > (depends on initialization scope) are initialized, otherwise, a RuntimeException is thrown. > 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. https://wiki.php.net/rfc/object-initializer#restrictions
Got it. Out of curiosity, would you be able to summarize why you believe your proposal was not accepted?
  109121
March 18, 2020 03:02 jakob@givoni.dk (Jakob Givoni)
On Mon, Mar 16, 2020 at 9:07 PM Jakob Givoni <jakob@givoni.dk> wrote:
> Thank you for your feedback so far! I've rewritten parts of the RFC substantially
(https://wiki.php.net/rfc/compact-object-property-assignment) so take another look if you too have ever wanted to create, populate and send an object inside a function call.