Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123547 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 C9A371A009C for ; Fri, 7 Jun 2024 19:27:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717788539; bh=5ehmv2634tj4uiOJZjEH1IxVovwIiNprj4FSPZD6SmE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=II5lAckahpYq8luqctEF9YME5GbUQ93d10UfwaCt8eJg1l8tem6EiIBAkNJ3nNLBQ gjoliEI9sI+1ZLsho70CffjuFxZ55PNf3b7CRIRQ/NSqYFM74T0fBv9qiR7u4zkWN8 xZhJwN2m8YTQQ4z7TIUTuf24WeCHo75N74oMozm1a1ZBHUqXvElXU55gqI4Ca7lK9F ZPBrobjHRqtf5lKdUDnkvLbAYwXQ1N60QRKiiV7R3T/prunvbESkPhrfhyrOclFC3p PBsfdH2zxyGkvAFsHDDCm9VAV+a0Y05ATqN3vroLc8Rou7po/FelGCDJ8cuSO+GxKI 1IZJLNWBYcQRg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A08CA180C2B for ; Fri, 7 Jun 2024 19:28: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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,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-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) (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:28:58 +0000 (UTC) Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-dfab2474152so2617032276.3 for ; Fri, 07 Jun 2024 12:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1717788471; x=1718393271; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Ij+3pFZ6P7F6AgBE9/6ELapHQcdImGot8ez23Yi+Q4I=; b=HyBOA1pH3996uSP7CxYc0x67kwV5bVV/ME+n3rhXWLxvgyV6voY5XM/NI7pWkhMrcg u+QscqIc8u1R/Bi9LB9YdlmW9oxIy5aLl5ta0lU5wBnk3ilUd/LpHWQ4qfc+JvOmYyf8 w7in2hQemQrfz20rPnYNcfrNZTLiTbE3VAfUM7uYvL7B8vRFfQmAuxyZt/pJ4DuZ8Gk2 ti+4YwWGtRAlJ7klBYrtWkTr1VbjKQQX4rrWucazyZeBtYRghEkDArnp9N47jLaoLUDj VedHzPtqrmKrW6yJcb8G48NU1lT9GEmcSkw080252cZC1TWVK+8JriUhZMffy3BsxNLX FLxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717788471; x=1718393271; h=content-transfer-encoding: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=Ij+3pFZ6P7F6AgBE9/6ELapHQcdImGot8ez23Yi+Q4I=; b=sqyzMriVWPT3Czz3KRYuUQWkiDwGvlK3OwH2tGdy0Q5wr7haQ6K15MPd/PlJLBK9Az YxQuz6g6uAgnUFSBpZSSFhvUGkFElwgAeqmtmFqgCxOA9+VOPB6YZwbCk3jBXSBAZF54 37zO1qLeQGlXkuPbXOcDStVIa8qmBUEs5gYGsn+XdMk0LxKeZFredgFeGqWPrew8QaAh dlZ++6g9dtiSMyv3ZQl1pPeXfQZ6NzwC3MGz0BPu5tmAEAbVHAch2PeJBnGdTlLyHTJ2 EmNyxbgNW6+Zfo4K6SGzzR5AQ4FAFxNMwqGV3WmfG4k9Vhvy4tsBmRfOr36uG6uhCcRT 882Q== X-Forwarded-Encrypted: i=1; AJvYcCWdt1p6OZTVhRJIlb7H28UJy8/O8wut0hpBEVBrd0eLpOcCYMwWKCB88oyFSmNr64i+EoIp+A5CKdQKZwiByN8TqY/9uwLPiw== X-Gm-Message-State: AOJu0Yz0xtMBjELxRrTrfFS4Ln8iBcwfUbH4ZMjBRrorXxDtdFT5mB8w zVuUqT0m/21SKq20EKEY5K1GiYQWunax9UGuqNOO+S0vFlb6ukT/r4U1fbmv3PGA5Vcryl9MuEo z+wgH91zWfFNiBOJrZc6lh+n+6fKxkidvX1eLgg== X-Google-Smtp-Source: AGHT+IGb3wd+9JdsRSoc0d99G+vmgzhyxN/AxdKmJUoyRfe3W1L3ozUmYI3PnBtFNkWqZ/tv0ijPGPNFQ9LV4dYjTnw= X-Received: by 2002:a25:744d:0:b0:dfa:528d:e8f4 with SMTP id 3f1490d57ef6-dfaf65c01c4mr3180784276.33.1717788471621; Fri, 07 Jun 2024 12:27:51 -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: Date: Fri, 7 Jun 2024 21:27:40 +0200 Message-ID: Subject: Re: [PHP-DEV] RFC proposal : "new" keyword shorthand To: Rob Landers Cc: Larry Garfield , php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: andreas@dqxtech.net (Andreas Hennings) On Fri, 7 Jun 2024 at 21:17, Rob Landers wrote: > > > > On Fri, Jun 7, 2024, at 21:02, Andreas Hennings wrote: > > On Fri, 7 Jun 2024 at 20:31, Larry Garfield wrot= e: > > > > 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 function that c= alls 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 n= ame in the namespace that is an alias for a new objects and forwards args t= o the constructor. This could be quite handy for value objects. > > >> > > >> I=E2=80=99m not necessarily a fan of magic or symbols, but just toss= ing 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). Bu= t > > > 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 PH= P > > > 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 defaul= t > > > behavior of this kind of interface to support immutability, compariso= n, > > > 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 th= at 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 f= atal. > > > > If we had 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 f= or 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 th= e 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 > > > > Hello Benoit. > others already pointed out problems with the `$obj =3D ~C();` syntax, > but also with the other option, `$obj =3D C();`. > > 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." > > 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. > > 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. > > -- Andreas > > > If we wanted a suffix, why not just use an empty ::? > > $frankenstein =3D Book::($isbn); > > =E2=80=94 Rob I am not strictly opposed to `$obj =3D C::()`. One problem I can imagine is `$obj =3D C::{$method}();`, where for some reason null or empty string '' is passed as $method. Currently, php will fail if this happens. But with the C::() syntax, it would then silently create a new instance. The same would happen if an unlucky find/replace removes the method name. Not sure if that's a blocker. -- Andreas