Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123545 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 0F81B1A009C for ; Fri, 7 Jun 2024 19:17:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717787935; bh=02TBpPmZjRRJ3qv5UImCos8LxlyiT+Ld7eSXwiNAClQ=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=cLAicbmHiKbgWYi68D6P68+JQm2MYBuUH5KNvlxrnA4d3ygMmn2ZSuHjpZfUJNaa8 Uy36hUwnO94Wm8p1ku91y3YDPK+Wii7wxUzQQWNdf5rOnqqgT5iN1BrNcOJfrpsbnY cFDhN+dr2THEWc5qh3FoJDTWXtsy1qjSFkFdE0092K67uIDv/uO7O2MZ7HiTH7KNrr oNeaJZSAceFeVTpJ18AhW3ngQ2nekvjH96NVng1YyNUOnHnkFFsK4ooRn8Qi98Wd9v xwctt4BIOrkbXXUORLXraJzT6CrtBOzoBNvHi5NYcbj8C04DgAeSqtS2q4Xg0vOp74 62IUc+xMUptrQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7BBE218073A for ; Fri, 7 Jun 2024 19:18:54 +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 wfout5-smtp.messagingengine.com (wfout5-smtp.messagingengine.com [64.147.123.148]) (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 19:18:53 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id EC5BD1C000FC; Fri, 7 Jun 2024 15:17:46 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute1.internal (MEProxy); Fri, 07 Jun 2024 15:17:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc: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=1717787866; x= 1717874266; bh=O3/qXBYD1FCdODQDruqXxrOHa3aaicKcY0lwdHzeYO0=; b=Z NFlEzC/X9diz0NQbQWEBs2XQ2gd7Dwo3VWVAExvcTEbrTujf2/tz9efAzR4vkcB5 A5kxzDF65H0s2DWCkyDSGZccGJc9N5JASyDi0+sH2vg5N3X4VzmTv31BXyqMVvmN hAYb2wBp6RNKDyB1RKdeP83y1sL5lkCl6h81dVognTBOtkJxu7x5suffoafEA/My uffrSpTyW4MFM2q1HDNgQ2s9uDa1qsx+XykTD+LnA33o4mGVnR6NR/cLKd+HT2Rb K4JYpJNKJSi2QFh+ZJ5iGNJIhgCN574jkfC4GKyA0jbrH+66a0GPxl2VuwkPfO8W 8hcxiJWV4O5fGyUlYbW2g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1717787866; x=1717874266; bh=O3/qXBYD1FCdODQDruqXxrOHa3aa icKcY0lwdHzeYO0=; b=eFOkByp9bmfFHdtiAtT4GkpfcMHtx/CKDISbRzICR8LV x86bdLSBfYfp/ei9XGtY2/+qwC/jHkmyxsntvwq4Px8X7CXExgroZzWDMdWoUDRA exsfraPI4X5pZa0D2YtjOg2Itx1FvtbbYzat5YIFZuwZEj+Vx1RkOobY7cSKJVrO MVthbTMFG9c8qu+Pjoc8wQSZmSGbsMag+ERdFH1s6sFCx80aCUU/Ll3LfXr234M/ PpNEbyHiEgoZZehN0IrXL1q/2/bWGgZP3cKsyP3+gC4e+dLp6EC1PQMsAl2RAj9N 6kTkjC3oljDIz96p2roazo/8qIAJbLOPA+sKkDk/Vg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedtuddguddutdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvvefutgesrgdtreerreerjeenucfhrhhomhepfdft ohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtf frrghtthgvrhhnpeelffelffehffdtvddvueeijeekledvueelhfeiueehfeevkeefuedu feeigedtffenucffohhmrghinhepshgtrghlrgdqlhgrnhhgrdhorhhgnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgv ugdrtghouggvsh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 2B65E15A0093; Fri, 7 Jun 2024 15:17:46 -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: In-Reply-To: References: <17bf2d84-1744-4879-8120-bd2e15315510@app.fastmail.com> <1cddd1cb-decf-4381-a290-149cd67bc6ba@php.net> <7b4e19ef-552d-4437-a1ad-5a2367e4008b@app.fastmail.com> Date: Fri, 07 Jun 2024 21:17:21 +0200 To: "Andreas Hennings" , "Larry Garfield" Cc: "php internals" Subject: Re: [PHP-DEV] RFC proposal : "new" keyword shorthand Content-Type: multipart/alternative; boundary=227d18ff266940548336471765272931 From: rob@bottled.codes ("Rob Landers") --227d18ff266940548336471765272931 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Fri, Jun 7, 2024, at 21:02, Andreas Hennings wrote: > On Fri, 7 Jun 2024 at 20:31, Larry Garfield w= rote: > > > > On Fri, Jun 7, 2024, at 5:56 PM, ericmann@php.net wrote: > > > > >> Instead of ~ (which reminds me of the pendulum of symbols to writ= ten 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 calls a constructor (kinda sorta like C# extension methods)? Somet= hing kinda like: > > >> > > >> class MyClass implements Invocable { > > >> public function __construct($i) {} > > >> } > > >> > > >> MyClass($i); > > >> > > >> Where the Invocable interface defines a function of the same clas= s 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 t= ossing it out there to spark ideas. > > >> > > >> =E2=80=94 Rob > > > -- > > > > > > I'm not a fan of using ~ for this shorthand, due to the same issues > > > that have been brought up elsewhere (i.e. it already has meaning).= But > > > the idea of a new interface auto-defining a function of the same n= ame > > > is attractive. It's something I've done manually to mirror code in= PHP > > > from other implementations existing in Scala leveraging case class= es. > > > > > > https://docs.scala-lang.org/tour/case-classes.html for anyone > > > unfamiliar with the construct: > > > > > > ``` > > > case class Book(isbn: String) > > > > > > val frankenstein =3D Book("978-0486282114") > > > ``` > > > > > > Doing something like this in PHP is a bit more ... verbose today: > > > > > > ``` > > > class Book > > > { > > > public function __construct(public string $isbn) {} > > > } > > > > > > function Book(string $isbn): Book > > > { > > > return new Book($isbn); > > > } > > > > > > $frankenstein =3D Book("978-0486282114") > > > ``` > > > > > > That's a lot of boilerplate for the shorthand to not require a `ne= w` > > > keyword. Which is to say, I both agree in principle with the RFC a= nd > > > think this would add value (particularly if I could extend the def= ault > > > behavior of this kind of interface to support immutability, compar= ison, > > > etc similar to Scala case classes). I'm just not a fan of the curr= ent ~ > > > proposal. > > > > > > ~Eric > > > > The issue with auto-generating a function constructor for a class is= that we still don't have function autoloading. Which means this: > > > > // Point.php > > > > #[MakeFactoryFunction] (or whatever) > > class Point { > > public function __construct(public int $x, public int $y) {} > > } > > > > // index.php > > > > $p =3D Point(4, 5); > > > > will look for a function named "Point", not find one defined yet, an= d fatal. > > > > If we had function autoloading, that would work, assuming a reasonab= le generic autoloader for that case could be written. Who was it that's= been talking about that for a while? :-) > > > > Or if we had some marker to indicate "this function call is a wrappe= r for a class so autoload it like a class", that could then trigger the = class autoloader. But... that's basically what the original ~ proposal = is, in the end. Though I agree that ~ is not a great symbol for it. PH= P has, sadly, run out of unused symbols. (I've run into this a couple o= f times now.) > > > > --Larry Garfield >=20 >=20 >=20 > Hello Benoit. > others already pointed out problems with the `$obj =3D ~C();` syntax, > but also with the other option, `$obj =3D C();`. >=20 > Personally I would prefer a suffix operator that would look similar to > a static method call. > This would make it a lot easier to switch between calling a static > factory and "new." >=20 > Unfortunately, `$obj =3D C::new();` is not an option, because "new" is= a > legal method name, so there could already be existing methods with > that name. > Also, `$obj =3D C::__construct();` would be just wrong. >=20 > But if we can find anything in that direction, I would much prefer it > over a prefix operator. > E.g. `$obj =3D C::+();` is currently not valid syntax, so it would be a > candidate, even though I don't really like it. >=20 > -- Andreas >=20 If we wanted a suffix, why not just use an empty ::? $frankenstein =3D Book::($isbn); =E2=80=94 Rob --227d18ff266940548336471765272931 Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable

=
On Fri, Jun 7, 2024, at 21:02, Andreas Hennings wrote:
On Fri, 7 Jun= 2024 at 20:31, Larry Garfield <larry@garfieldtech.com> wrote:
>
<= div>> On Fri, Jun 7, 2024, at 5:56 PM, ericmann@php.net wrote:
>
= > >> 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 fu= nction that calls a constructor (kinda sorta like C# extension methods)?= Something kinda like:
> >>
> &g= t;> class MyClass implements Invocable {
> >>&= nbsp;  public function __construct($i) {}
> >&g= t; }
> >>
> >> MyClass($i)= ;
> >>
> >> Where the Invo= cable interface defines a function of the same class name in the namespa= ce that is an alias for a new objects and forwards args to the construct= or. This could be quite handy for value objects.
> >= >
> >> I=E2=80=99m not necessarily a fan of ma= gic or symbols, but just tossing it out there to spark ideas.
<= div>> >>
> >> =E2=80=94 Rob
> > --
> >
> > I'm not a= fan of using ~ for this shorthand, due to the same issues
> > that have been brought up elsewhere (i.e. it already has mean= ing). But
> > the idea of a new interface auto-defin= ing a function of the same name
> > is attractive. I= t's something I've done manually to mirror code in PHP
>= ; > from other implementations existing in Scala leveraging case clas= ses.
> >
> > unfam= iliar with the construct:
> >
> >= ; ```
> > case class Book(isbn: String)
> >
> > val frankenstein =3D Book("978-04862= 82114")
> > ```
> >
> > Doing something like this in PHP is a bit more ... verbose t= oday:
> >
> > ```
= > > class Book
> > {
> >&n= bsp;    public function __construct(public string $isbn) = {}
> > }
> >
> = > function Book(string $isbn): Book
> > {
> >     return new Book($isbn);
> > }
> >
> > $fran= kenstein =3D Book("978-0486282114")
> > ```
> >
> > That's a lot of boilerplate for = the shorthand to not require a `new`
> > keyword. Wh= ich is to say, I both agree in principle with the RFC and
= > > think this would add value (particularly if I could extend the= default
> > behavior of this kind of interface to s= upport immutability, comparison,
> > etc similar to = Scala case classes). I'm just not a fan of the current ~
&= gt; > proposal.
> >
> > ~Eric=
>
> The issue with auto-generating a = function constructor for a class is that we still don't have function au= toloading.  Which means this:
>
>= // Point.php
>
> #[MakeFactoryFunctio= n] (or whatever)
> class Point {
>&nbs= p;  public function __construct(public int $x, public int $y) {}
> }
>
> // index.php
>
> $p =3D Point(4, 5);
&g= t;
> will look for a function named "Point", not find o= ne defined yet, and fatal.
>
> If we h= ad function autoloading, that would work, assuming a reasonable generic = autoloader for that case could be written.  Who was it that's been = talking about that for a while? :-)
>
>= ; Or if we had some marker to indicate "this function call is a wrapper = for a class so autoload it like a class", that could then trigger the cl= ass autoloader.  But... that's basically what the original ~ propos= al is, in the end.  Though I agree that ~ is not a great symbol for= it.  PHP has, sadly, run out of unused symbols.  (I've run in= to this a couple of times now.)
>
> --= Larry Garfield



Hello Benoit.
others already pointed out problems with t= he `$obj =3D ~C();` syntax,
but also with the other option= , `$obj =3D C();`.

Personally I would prefe= r a suffix operator that would look similar to
a static me= thod call.
This would make it a lot easier to switch betwe= en calling a static
factory and "new."

<= /div>
Unfortunately, `$obj =3D C::new();` is not an option, because = "new" is a
legal method name, so there could already be ex= isting methods with
that name.
Also, `$obj =3D= C::__construct();` would be just wrong.

Bu= t if we can find anything in that direction, I would much prefer it
<= /div>
over a prefix operator.
E.g. `$obj =3D C::+();` = is currently not valid syntax, so it would be a
candidate,= even though I don't really like it.

-- And= reas


If we want= ed a suffix, why not just use an empty ::?

= $frankenstein =3D Book::($isbn);

=E2=80=94 Rob
--227d18ff266940548336471765272931--