Re: [PHP-DEV] Re: [RFC] Object Initializer

This is only part of a thread. view whole thread
  107172
September 16, 2019 14:57 rowan.collins@gmail.com (Rowan Tommins)
On Mon, 16 Sep 2019 at 15:37, Michał Brzuchalski <michal@brzuchalski.com>
wrote:

> > >> The problem with that is that you need an extra static method to make use >> of it, and you still need to get the arguments into that method. It might >> be useful occasionally, but it still doesn't help constructors which are >> setting a large number of private / protected properties. >> >> > This RFC is not trying to help those constructors but tries to simplify > instantiation objects and initializing properties > there where any kind of constructor won't help, but rather would be > unnecessary at all. >
I realize that, I was responding to a specific point: you said that the syntax would work for protected or private properties if used where those are visible. I was saying that I don't think that combination would be used very often, so it's easiest to just discuss the public property case.
> You wouldn't want to put 15+ arguments in your constructor to initialize > public properties which > don't need other validation than proper type, right? > > Even if it would be just adding "public" keyword in front of them. >
Why not? You've got to list those 15 properties somewhere; if the syntax was such that you only needed to list them once, it makes no difference whether we call the result "class initializer" or "automatic constructor with 15 named parameters", IMO.
> > >> Either of those, with named parameters, would be almost indistinguishable >> from object initializers at the call site. Depending on the syntax chosen, >> it might be as similar as: >> >> // Call initializer, requires public properties >> new Employee { age => 42, name => 'John Smith' }; >> // Call constructor, requires special constructor definition >> new Employee( age => 42, name => 'John Smith' ); >> >> > Last RFC treating about named arguments has similar syntax with curly > braces, but all together with previous ones > tries to solve the issue through additional syntax inside parentheses, > which means both features can coexist together. > > Calling instantiation always used parentheses as the way to pass > constructor arguments let's keep it that way. > Using object-initializer would use curly braces - just like it's used to > be solved in other languages. >
My intention here was just to show that using named parameters would be just as concise as using an object initializer; I just picked a pair of syntaxes that were as similar as possible to illustrate that.
> > >> >> That would require multiple new features, though, so initializers might be >> more achievable in the short term, and perhaps there is room for both, >> particularly if support for getters and setters improves. >> >> > Here again, IIRC you're trying to solve the issue which is off-topic. > Improving protected and private properties initialization through > constructor is not the main target of current RFC. >
I don't think it's off-topic to consider whether a related feature would make this one redundant. However, you've picked a weird sentence to reply to, because I'm agreeing with you, that the two features could exist side by side without being redundant. Regards, -- Rowan Tommins [IMSoP]
  107174
September 16, 2019 15:16 michal@brzuchalski.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=)
Hi Rowan,

pon., 16 wrz 2019 o 16:57 Rowan Tommins collins@gmail.com>
napisał(a):

> > >> >>> >>> That would require multiple new features, though, so initializers might >>> be >>> more achievable in the short term, and perhaps there is room for both, >>> particularly if support for getters and setters improves. >>> >>> >> Here again, IIRC you're trying to solve the issue which is off-topic. >> Improving protected and private properties initialization through >> constructor is not the main target of current RFC. >> > > > > I don't think it's off-topic to consider whether a related feature would > make this one redundant. However, you've picked a weird sentence to reply > to, because I'm agreeing with you, that the two features could exist side > by side without being redundant. >
Sorry for that, the quoted sentence was left unintentionally and yes, it's not off-topic. Had to rethink what I was trying to say, but I do think both these features could exist. Regards, Michał Brzuchalski
  107181
September 16, 2019 21:55 larry@garfieldtech.com ("Larry Garfield")
On Mon, Sep 16, 2019, at 10:16 AM, Michał Brzuchalski wrote:
> Hi Rowan, > > pon., 16 wrz 2019 o 16:57 Rowan Tommins collins@gmail.com> > napisał(a): > > > > > > >> > >>> > >>> That would require multiple new features, though, so initializers might > >>> be > >>> more achievable in the short term, and perhaps there is room for both, > >>> particularly if support for getters and setters improves. > >>> > >>> > >> Here again, IIRC you're trying to solve the issue which is off-topic. > >> Improving protected and private properties initialization through > >> constructor is not the main target of current RFC. > >> > > > > > > > > I don't think it's off-topic to consider whether a related feature would > > make this one redundant. However, you've picked a weird sentence to reply > > to, because I'm agreeing with you, that the two features could exist side > > by side without being redundant. > > > > Sorry for that, the quoted sentence was left unintentionally and yes, it's > not off-topic. > Had to rethink what I was trying to say, but I do think both these features > could exist. > > Regards, > Michał Brzuchalski
I am not sure I agree here. As I noted earlier, we're running out of sigils. Suppose initializer syntax used: new Employee{prop = "val"}; Now later we want to add named parameters. What happens if we use the same syntax? Does it mean the same thing, really? Or does the meaning of that call syntax vary depending on whether the constructor is defined in such a way to make it support named parameters? What happens when it could mean either, but the result would be different? Or do we have to guarantee that the result is the same, even if that means limiting one or the other? That could be avoided by using some other syntax for named parameter calls, say: new Employee({ prop = "val"}); But now we have two syntaxes that do *almost* the same thing, but with subtle differences, and the existing positional syntax, which means there's now 3 ways to create an object that look very similar visually but mean slightly different things. That's... not good. I still hold that initializers as described, even though I like the problem they're solving being solved, are a strict subset of the combination of named parameters and auto-constructor promotion. If we had those, we would have more functionality and initializer functionality comes for free, and it would be more self-evident what was going on, with fewer syntax variations. Whereas adding initializers now as a one-off would likely make adding those later more difficult. Thus I would rather see time spent on adding those than on a one-off syntax. (And yes, I fully realize I am saying that as someone not currently working on any of the above syntaxes and so I'm talking about other people doing work, etc.) --Larry Garfield