Re: Proposal fo "Code-free constructors declaration"
| From: | Rowan Collins | Date: | Fri, 25 Jan 2019 11:54:33 +0000 |
| Subject: | Re: Proposal fo "Code-free constructors declaration" | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-103846@lists.php.net to get a copy of this message | ||
On Fri, 25 Jan 2019 at 11:05, Nikita Popov <nikita.ppv@gmail.com> wrote:
> class Point {
> public int $x;
> public int $y;
> public int $z;
> }
>
> // (syntax just a dummy)
> $origin = new Point { x = 0, y = 0, z = 0 };
>
> Where the object initialization syntax ensures that all properties that
> don't have defaults are initialized. Especially now that we have typed
> properties and it is feasible to have data objects with just public typed
> properties, I think that this is the way to avoid ctor boilerplate, rather
> than making it simpler to write that boilerplate.
>
The big difference here is that you are making the short-hand the
responsibility of the caller, rather than the definer. I suspect there's a
Venn diagram of use cases where the two features would be useful.
In particular, the call-site syntax you have there would work really well
for a value type, and could go with having full support for that (e.g. have
the resulting object be immutable / copy-on-write); it works less well for
something like dependency injection, where the properties would normally be
private, and you might want to combine the short-hand with some normal
constructor logic.
Regards,
--
Rowan Collins
[IMSoP]