Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123550 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 3FB5F1A009C for ; Fri, 7 Jun 2024 20:06:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717790878; bh=ERBGWYGy7+Q8sn2kPBcmVdR0e8yAbEx9cCdWjRKq3sA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bKnFxcJDq47UqbeNn6CxP2GEMuEfuzhMtP+ESImamIqDiN3uOGuPP+FDeDomaE48s 9wLMIxleDwC0nadKPdQHhdiF29dzZz4hv6p8oFDv24IZb1hsSPTtRY3KqlKt9MwmRR O3sMeg0fGI+Rxvt+DIG0CBOmJcASl12K44/O+VD9Iug4+s+E34lkC8CumGjZKOIxou nj5PrPEId+KV4Mnzp046sQ74OpENef4QbdTR93ijppZjIPTJgnjW8xxpSJU16hL2cT kolXK5ceqocTZBExDiVQIvnv+MsisyoezQ+O9YpdVTNq2BLlc8xp7qo1epaXw+rOfM gRV0Twp3bRjng== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8FFD2180CE1 for ; Fri, 7 Jun 2024 20:07:57 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, 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 mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (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 20:07:57 +0000 (UTC) Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2ea8fff1486so23539661fa.1 for ; Fri, 07 Jun 2024 13:06:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717790810; x=1718395610; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Bj1m1AnYUvdMFzLa98/0Lu+UelrjlVrS5lDrG3AKfNA=; b=KYjo63qpRXcnTyh7+qlKNSvsy7KBVQRs3/RVnPYNFuO84yyQBv9BvcBvtVF0b9A3ff p6FkidPFAKZyJM0SN5LoYm5u7O/q8Q+Wy2xIMG5Zugs4Vx0QcECneGpLadNlwToD/h8n FkpBw91NIQ5ZBLzEc71Xo2nJ54uUZ+Kp+muHQaMDPzzTfRPsFx7F8PGKY673VygWtqby 0J1of6juMn/WLsHNsJlH7OK/zNQiK+jZqSE7mOLSeN5BdHP02lVZNQ1V9hwCfoYd2gBc jsx/Y1gvcYPD4j6JQY/fCpZglPRNJEykIwi+EapO68ewo2Xb1FYqGedACNf9rvMcHOdD gz/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717790810; x=1718395610; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Bj1m1AnYUvdMFzLa98/0Lu+UelrjlVrS5lDrG3AKfNA=; b=TwdTn5EIevl41JrJDGLwftKJGuLJE4Jj8VX9lCw4nQznyKXy0Mnq4uH/dzyXcG2XLx nU0HRCyG4YIzdPN5vdDRMuiPnC1xAunxlZvKBm9Nqeiy85FfE6tA5ujAjowNz++Tz5BE BeU1odESQlRpHYblUK/dTfXavuoSmRH9ooeyAsngAJ9KYVgrMX76UZ57eoCXK09vPMtQ 2NMEbB+Mas1xCKFgvl7c6QLGUCSkIeCa+lDb/w8g9078daaO9jZ1o3qcro1A0k3sVH91 s2xz6UI5GCpjDnteUPV8FczqJKqn9kwek6t4Uj836UukOjKULBXaOY4aP1keSwK63Bmj s7VA== X-Gm-Message-State: AOJu0Yz/6VefAH2bHLgKqLz5UN6Xe+zvGuoSej/rDbCWGrjJSOtplg4D 9HnSw/Tv0THnDLOesyWWzr5iYR01zYO07UAZRfvrt8EdL9kGqYasmCRL0cHkK42yd0cN9MpohjT Mh9BlwSzIP1Qm+Ar+uqQ46w2JHttpH5fy X-Google-Smtp-Source: AGHT+IFbQDq4OXb2hAUfIOY6t3w6terU+hQT3hwY8nlhnPdDJjy3HqRNjtcIGtHn2zb3hCNjicoOmf0UuKmSbFrhQq4= X-Received: by 2002:a2e:b790:0:b0:2ea:bab1:59e4 with SMTP id 38308e7fff4ca-2ead01e351bmr19510391fa.18.1717790809442; Fri, 07 Jun 2024 13:06:49 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <17bf2d84-1744-4879-8120-bd2e15315510@app.fastmail.com> <1cddd1cb-decf-4381-a290-149cd67bc6ba@php.net> <7b4e19ef-552d-4437-a1ad-5a2367e4008b@app.fastmail.com> In-Reply-To: <7b4e19ef-552d-4437-a1ad-5a2367e4008b@app.fastmail.com> Date: Fri, 7 Jun 2024 22:06:37 +0200 Message-ID: Subject: Re: [PHP-DEV] RFC proposal : "new" keyword shorthand To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000fed8ac061a525692" From: condaminet.benoit@gmail.com (=?UTF-8?Q?Beno=C3=AEt_Condaminet?=) --000000000000fed8ac061a525692 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Le ven. 7 juin 2024, 21:31, Larry Garfield a =C3=A9crit : > 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 t= o > 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)? Something kinda like: > >> > >> class MyClass implements Invocable { > >> public function __construct($i) {} > >> } > >> > >> MyClass($i); > >> > >> Where the Invocable interface defines a function of the same class nam= e > 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 tossin= g 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 name > > is attractive. It's something I've done manually to mirror code in PHP > > from other implementations existing in Scala leveraging case classes. > > > > 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 `new` > > keyword. Which 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 support immutability, comparison, > > etc similar to Scala case classes). I'm just not a fan of the current ~ > > 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, and > fatal. > > If we had function autoloading, that would work, assuming a reasonable > generic autoloader for that case could be written. Who was it that's bee= n > 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 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. PHP has, > sadly, run out of unused symbols. (I've run into this a couple of times > now.) > > --Larry Garfield > Thank you all for these answer, I miss the not bitwise operator... Sorry about that. Totally agree, look like we run out of unused symbol. And that it's maybe not the ideal solution to add a new one or combination of existing. Benoit. > --000000000000fed8ac061a525692 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


Le ven. 7 juin 2024, 21:31, Larry Garfield <lar= ry@garfieldtech.com> a =C3=A9crit=C2=A0:
On Fri, Jun 7, 2024, at 5:56 PM, ericmann@php.net wrote:

>> Instead of ~ (which reminds me of the pendulum of symbols to writt= en 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 kinda= like:
>>
>> class MyClass implements Invocable {
>>=C2=A0 =C2=A0public 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 to= ssing it out there to spark ideas.
>>
>> =E2=80=94 Rob
> --
>
> I'm not a fan of using ~ for this shorthand, due to the same issue= s
> 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 name =
> is attractive. It's something I've done manually to mirror cod= e in PHP
> from other implementations existing in Scala leveraging case classes.<= br> >
>
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
> {
>=C2=A0 =C2=A0 =C2=A0public function __construct(public string $isbn) {}=
> }
>
> function Book(string $isbn): Book
> {
>=C2=A0 =C2=A0 =C2=A0return 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 and <= br> > think this would add value (particularly if I could extend the default=
> behavior of this kind of interface to support immutability, comparison= ,
> 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 w= e still don't have function autoloading.=C2=A0 Which means this:

// Point.php

#[MakeFactoryFunction] (or whatever)
class Point {
=C2=A0 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,= and fatal.=C2=A0

If we had function autoloading, that would work, assuming a reasonable gene= ric autoloader for that case could be written.=C2=A0 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 = class autoloader.=C2=A0 But... that's basically what the original ~ pro= posal is, in the end.=C2=A0 Though I agree that ~ is not a great symbol for= it.=C2=A0 PHP has, sadly, run out of unused symbols.=C2=A0 (I've run i= nto this a couple of times now.)

--Larry Garfield


Thank you all for these answer,= I miss the not bitwise operator... Sorry about that.=C2=A0

Totally agree, look like we run out o= f unused symbol. And that it's maybe not the ideal solution to add a ne= w one or combination of existing.=C2=A0

Benoit.=C2=A0
--000000000000fed8ac061a525692--