Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123541 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id CFE9D1A009C for ; Fri, 7 Jun 2024 17:35:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717781819; bh=OxpGWvgLvtMCE9W9E5Z/3LF2bydqWZmdKQloebQq+4w=; h=In-Reply-To:References:Date:From:To:Subject:From; b=WeQ1RebrXeW9NPzVSDGHwbZkEjoL4jOEowHuBWtkBzWmuyMpWHDvkRopCyWRFyq8W Slpzf0XRjqO5bTPWkAfSJEiA/yu0mNcDhxr/4pJvJp8c6hLNc87O/YQ37Zy0XcTf1u fSo807kunj9EN27YxCOf2JMpbBD2A5r2cyeZO2hyHqomvvc2PxBuEhldaH0nDSCdxP R1a58JksWs6KS7REEc06Vdb3ly4zIB4KH3RbxJqSAaNmoa8DhfucGHIOR1GpOIH8R2 BgyBvxsn7iR8bTkQ0taU6Jdvxux7+iLdT609UTj8+Stz+IsIteA5JuzTYkI0ylJcxW YmrvL0oYDBYKA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 28A36180686 for ; Fri, 7 Jun 2024 17:36:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout8-smtp.messagingengine.com (fout8-smtp.messagingengine.com [103.168.172.151]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 7 Jun 2024 17:36:57 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id 3C65D13800FD for ; Fri, 7 Jun 2024 13:35:51 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute1.internal (MEProxy); Fri, 07 Jun 2024 13:35:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1717781751; x=1717868151; bh=q02GllDOws naN58DWblREjHAStbEcjBY+RjZyGxXUy8=; b=C0gGIrQCTmjqwxLbi8gzj3o0PG i0xsgvnoE8+dAmLLX3STWTyfxaaJ7k1ls8vgmBf5IievTQXwtmxEOwOfrhtX6gBc Vd7eWD8K3fahFfyShnxzmNP8SEwzLn3CrIpRGErijHVGtffkCzZTkz+LZL5SJ3yG gWV7mjNkj0clBThITdNju9BKJc8yvDQROtf3KrtSrIUsvDyJo+0RhQkk/O+DITaP 4NgWze0hISyt/pu5kRdeLOBSqKS5V3xgyA5WvwvLsBZRUONRUnZlF25Dcf6xwJgf lJ00ilEK3DWbGp55DYk4HavwZYifdDXQocTDS1fd2v8GeDxIHxPyhBnClGVQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1717781751; x=1717868151; bh=q02GllDOwsnaN58DWblREjHAStbE cjBY+RjZyGxXUy8=; b=RhaZqkWZdWWQKF1zIxDcWiupfVYNjTWiqulnh55Mevj1 LI8WibzOjAojQ84zbafHdC1uBN6kxeRuvyS56xPpA56DRbdFiDDwAtVR1iepUN3V I54SWGnYdWy4yZ+nlGPGkEZUlvo5B2IiBaWcTM3Nrj7izhomt0w/WufaHSe0n1Kp CN3gxNm/JEoJSzyLX5itnLO2wCliBNgoFNZCy2qai9JRofG8pljuvjOjpcfOFqhc v+SERSB5gcgjbuRRD2sX2WHWxQ0isHLS2rEofocVee98W/ALXE86ZdLuedT/DasO 6XpUwOWJ/r+yiiXAOsBZjjC8E88Ljn8Ufm/Immqa9w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedtuddgkeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsegrtd erreerreejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorhhosgessghothht lhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepfeefudfhudduieekkedugffhud fgleejgfekgefhvdeikeelvddvjeehteegteegnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvsh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id BD76915A0092; Fri, 7 Jun 2024 13:35:50 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-497-g97f96844c-fm-20240526.001-g97f96844 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <17bf2d84-1744-4879-8120-bd2e15315510@app.fastmail.com> In-Reply-To: References: Date: Fri, 07 Jun 2024 19:35:30 +0200 To: internals@lists.php.net Subject: Re: [PHP-DEV] RFC proposal : "new" keyword shorthand Content-Type: multipart/alternative; boundary=694884b1678d4a00bbe88fb40228deb5 From: rob@bottled.codes ("Rob Landers") --694884b1678d4a00bbe88fb40228deb5 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Fri, Jun 7, 2024, at 18:03, Beno=C3=AEt Condaminet wrote: > Hello, >=20 > Following the RFC process, I'm sending this to propose a PHP change. M= ore precisely a new zend language token.=20 >=20 > This is somehow linked to the recently accepted RFC called "new MyClas= s()->method() without parentheses", the goal is to introduce a shorthand= for the "new" keyword.=20 >=20 > *Motivations :* > The new keyword sometime has a bad DX, for example when chaining Class= instantiation like this : > $foobar =3D new Foo( new Bar(), new Etc()); >=20 > We can quickly have very long class object construction in some case.=20 >=20 > In lot of Framework, to improve DX, static function construct are ofte= n used to avoid the new keyword, some create additional object builders,= etc. >=20 > As a first try, I start updated code to completely make the new keywor= d optional, like in Dart language for example, but it require very big c= hange, with lot of impact (collision with function).=20 >=20 > So here is *my proposal : * >=20 > Add a "new" shorthand, using the tilde character : "~" > I made a POC, and it works well, declaring a new language token T_SHOR= T_NEW that simply reuse ZEND_AST_NEW under the hood.=20 >=20 > Here are some example of what it may look like for userland, through s= ome tests I wrote :=20 > --TEST-- > New keyword shorthand > --FILE-- > class A { > public function b() { > echo "I'm B"; > } > } >=20 > ~A()->b(); >=20 > ?> > --EXPECT-- > I'm B > Other with nested :=20 >=20 > --TEST-- > New keyword shorthand encapsulation > --FILE-- > class Foo { > public function __construct( > public string $name > ) {} > } >=20 > class Bar { > public function __construct( > public Foo $foo > ) {} > } >=20 > $bar =3D ~Bar(~Foo("I'm foo in bar")); > echo $bar->foo->name; >=20 > ?> > --EXPECT-- > I'm foo in bar > As a last word, just wanted to add it will work on nested Attribute to= o, so for example this nested Attribute used in Doctrine :=20 >=20 > #[AttributeOverrides([ > new AttributeOverride( > name: "id", > column: new Column(name: "guest_id", type: "integer", length: = 140) > ), > new AttributeOverride( > name: "name", > column: new Column(name: "guest_name", nullable: false, unique= : true, length: 240) > )] > )] >=20 > Will also work like this :=20 > #[AttributeOverrides([ > ~AttributeOverride( > name: "id", > column: new Column(name: "guest_id", type: "integer", length: = 140) > ), > ~AttributeOverride( > name: "name", > column: new Column(name: "guest_name", nullable: false, unique= : true, length: 240) > )] > )] >=20 > About implementation, it should'n have any impact on SAPI or extension= , the new keyword will still exist, it's just a new shorthand. >=20 > I think you get the point, is it something that some of you would be i= nterested in? I would be happy to make a proper RFC proposal.=20 >=20 > Just registered a wiki account : benconda >=20 > Thank you, >=20 > -- > Cordialement, > Benoit Condaminet Instead of ~ (which reminds me of the pendulum of symbols to written to = symbols and back again every 10ish years; =E2=80=9Cor=E2=80=9D vs =E2=80= =9C||=E2=80=9D), why not make a shorthand way to write a function that c= alls a constructor (kinda sorta like C# extension methods)? Something ki= nda like: class MyClass implements Invocable { public function __construct($i) {} } MyClass($i); Where the Invocable interface defines a function of the same class name = in the namespace that is an alias for a new objects and forwards args to= the constructor. This could be quite handy for value objects. I=E2=80=99m not necessarily a fan of magic or symbols, but just tossing = it out there to spark ideas.=20 =E2=80=94 Rob --694884b1678d4a00bbe88fb40228deb5 Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable

=
On Fri, Jun 7, 2024, at 18:03, Beno=C3=AEt Condaminet wro= te:
Hello,

Following the RFC pro= cess, I'm sending this to propose a PHP change. More precisely a new zen= d language token. 

This is somehow lin= ked to the recently accepted RFC called "new MyClass()->method() with= out parentheses", the goal is to introduce a shorthand for the "new" key= word. 

Motivations :
<= div>The new keyword sometime has a bad DX, for example when chaining Cla= ss instantiation like this :
$foobar =3D new Foo( new Bar(= ), new Etc());

We can quickly have very lon= g class object construction in some case. 

=
In lot of Framework, to improve DX, static function construct are o= ften used to avoid the new keyword, some create additional object builde= rs, etc.

As a first try, I start updated co= de to completely make the new keyword optional, like in Dart language fo= r example, but it require very big change, with lot of impact (collision= with function). 

So here is m= y proposal : 

Add a "new" shorthan= d, using the tilde character : "~"
I made a POC, and it wo= rks well, declaring a new language token T_SHORT_NEW that simply re= use ZEND_AST_NEW under the hood. 

Here are some example of what it may look like for userland, through so= me tests I wrote : 
--TEST--
New keyword s= horthand
--FILE--
<?php
class A {
=
public = function b() {
echo "I'm B";
}
}

~A()->b();

?>
--EXPEC= T--
I'm B
Other with nested : = ;

--TEST--
New keyword shorthand= encapsulation
--FILE--=
<?php=
class Fo= o {
p= ublic function __construct(
public string $name
) {}
}

class Bar {
public function __construct(
public Foo $foo=
) {}<= br>}

$bar =3D ~Bar(~Foo("I'm= foo in bar"));
echo $bar->foo->name;

?>
--EXPECT--
I'm foo in bar
As a last word, just wanted to add it will work on nested Attribute too= , so for example this nested Attribute used in Doctrine : 

#[AttributeOverrides([
  &n= bsp; new AttributeOverride(
        na= me: "id",
        column: new Column(n= ame: "guest_id", type: "integer", length: 140)
  &nbs= p; ),
    new AttributeOverride(
&= nbsp;       name: "name",
    &nb= sp;   column: new Column(name: "guest_name", nullable: false, uniqu= e: true, length: 240)
    )]
)]

Will also work like this : 
#[AttributeOverrides([
    ~Attribu= teOverride(
        name: "id",
        column: new Column(name: "guest_id",= type: "integer", length: 140)
    ),
<= div>    ~AttributeOverride(
     =   name: "name",
        column: = new Column(name: "guest_name", nullable: false, unique: true, length: 24= 0)
    )]
)]
<= br>
About implementation, it should'n have any impact on SAPI = or extension, the new keyword will still exist, it's just a new shorthan= d.

I think you get the point, is it so= mething that some of you would be interested in? I would be happy to mak= e a proper RFC proposal. 

Just registe= red a wiki account : benconda

Thank y= ou,

--
Cordialement,
Ben= oit Condaminet

Instead of ~ (which reminds me of the pendulum of symbols to wr= itten to symbols and back again every 10ish years; =E2=80=9Cor=E2=80=9D = vs =E2=80=9C||=E2=80=9D), why not make a shorthand way to write a functi= on that calls a constructor (kinda sorta like C# extension methods)? Som= ething kinda like:

class MyClass implements= Invocable {
  public function __construct($i) {}
=
}

MyClass($i);
Where the Invocable interface defines a function of the same= class name in the namespace that is an alias for a new objects and forw= ards args to the constructor. This could be quite handy for value object= s.

I=E2=80=99m not necessarily a fan of mag= ic or symbols, but just tossing it out there to spark ideas. 
=

=E2=80=94 Rob
--694884b1678d4a00bbe88fb40228deb5--