Re: RFC proposal : "new" keyword shorthand

From: Date: Fri, 07 Jun 2024 21:47:40 +0000
Subject: Re: RFC proposal : "new" keyword shorthand
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-123552@lists.php.net to get a copy of this message
On Fri, Jun 7, 2024, at 9:34 PM, Erick de Azevedo Lima wrote: > Hi all. > >> Em sex., 7 de jun. de 2024 às 17:53, Pierre <pierre-php@processus.org> escreveu: >> I do agree with you that when you use value objects, you need it a lot, >> but I'd much prefer having a JS like value objects initializer syntax, >> such as Point {x: 1, y: 2} syntax than creating another way >> to call >> the constructor, which already has a mastered and comprehensive syntax >> which is simply new. > > I also think that new (4 chars, if we count the blank space) is > short > enough. > But a JS-like syntax that's already known for representing objects > could be used. > I'm sympathetic to something like this (changing the OP examples): > > class A { > public function b() { > echo "I'm B"; > } > } > > A::{}->b(); > > > class Foo { > public function __construct( > public string $name > ) {} > } > > class Bar { > public function __construct( > public Foo $foo > ) {} > } > > $bar = Bar::{ Foo::{ "I'm foo in bar" } }; > echo $bar->foo->name; > > > #[AttributeOverrides([ > AttributeOverride::{ > name: "id", > column: Column::{ name: "guest_id", type: "integer", length: > 140 } > }, > AttributeOverride::{ > name: "name", > column: Column::{ name: "guest_name", nullable: false, unique: > true, length: 240 } > }] > )] > > > #[AttributeOverrides([ > AttributeOverride::{ > name: "id", > column: Column::{ name: "guest_id", type: "integer", length: > 140 } > }, > AttributeOverride::{ > name: "name", > column: Column::{ name: "guest_name", nullable: false, unique: > true, length: 240 } > }] > )] > > -- > Erick We already have named arguments, which gives nearly the same syntax. Writing directly to properties like that makes no sense in a language that has class-private properties and internal constructors. It's fine in Rust or Go that don't have internal constructors, but it's just not a good fit in a PHP-like language. And new Foo(a: 'A", b: 'B") is already perfectly readable and self-documenting. Yet another constructor-invoke syntax on top of that would offer nothing. --Larry Garfield

« previous php.internals (#123552) next »