Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123543 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 37EE11A009C for ; Fri, 7 Jun 2024 18:30:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717785108; bh=q9NoTIq7zMxl/EmFkkyE6juM3iuNHeJEH0VxIf1plaM=; h=In-Reply-To:References:Date:From:To:Subject:From; b=OdZs/m2liQDIgRVkX9nxNm3T98T6hgybELRIVMlkIyBqmrP+bHSH2GvoIMZANwhIn 3YgjcDTj9kxwp5/dzvgBGFhw/YJ8rL3ME2rPCBpFw1ABOFgmjLHKX/FcrBWx2K0WcW W8JHilsVfbtrqfH1iKAT4Bev0ceP/iVp6YXk1pWEVhFWomurFm9RLfDaJRvFlH0x4c GZEdyweT7QRqi3QkXhu5m5sxQJt/8oYi8lkShmIwERB1GaEZlKcxFQiUZ63n6GweFR 9Yl4KWIeUyHFesTcQ39BkVZSZCnI8lBDeypJWUkLVapEQuY8nBChWPEGZCfGbcUBI8 pWkmxtSomeAqg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6F8E518054F for ; Fri, 7 Jun 2024 18:31:47 +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,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE,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 18:31:46 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id 36DBD1C0019C for ; Fri, 7 Jun 2024 14:30:39 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Fri, 07 Jun 2024 14:30:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding: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=fm1; t=1717785038; x=1717871438; bh=APbZGRKdSkmMmn5zBSuNk Usf9eAcKaZ6/B1uRvPLqII=; b=DmTPNUpr3A5e63BPSMKjVR+waTzdwMdBwHjUE gSQC8dyJwm/SLR8xnR4XXobnG8ESmEI4K4hNn72XmxmbzPTxmfUnIrwqFrxB22/J RBPRMKlDsI+QL4zwuBJWXAzYs9Y6s5YMjKCf6kycj3Pg70/8KHxsHSfrSIE/MeCz J7qYbXGiunag0i28/zXmQvL2snm7rKf0Uz6YaaITazmkdMsTNZo5tS9FaaIXRA81 wW1VBLMe2pX6ElqfaJNg/UYcQToba2bPukhw1LwQM3lXiQ052ybz/94GE0jQiJgW b7f3rtZ7hPf56wbE9b+LD5eh28ctVaJxW19NbfYPFRsi/Dbeg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=1717785038; x= 1717871438; bh=APbZGRKdSkmMmn5zBSuNkUsf9eAcKaZ6/B1uRvPLqII=; b=l jzPlL9qTT+a4Q4tdKHYXMHv0M5ilyqR/NN8IQ0/xK7y+9XgjTvldJ/Bx5v/0RVZU rKOHvegY7rKtOXedo6OifBqjYNm5s5skBYoDpDXgnF7WaC6xb6AnCXV87LfZOwRb f5JXJ7Jr2Jppq+o7pOG8+3KbX9c+sYhvr0BsTM4rF9yTTTtTd4pJAdo4ENC1ooVv qvEcbyNsTkAsbZBSQLEHgYpPh98oIkKJFwx7sAntNrXMmFtk+p52gMaWNdbVgSEP /eGDdxI1eG6RRjaosy/72cNy4csyUzXnEe8c+4fQj6RuGxtC48KVPRZVNH6qH6rm xZ3wFZlODFH0GLKvBzPgA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedtuddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfn rghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrd gtohhmqeenucggtffrrghtthgvrhhnpeethfdvueffjefggfdvffelheekgeeghfejfeff veefgeelhfdvueefudevheehgfenucffohhmrghinhepshgtrghlrgdqlhgrnhhgrdhorh hgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgr rhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 5C8A01700093; Fri, 7 Jun 2024 14:30:38 -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: <7b4e19ef-552d-4437-a1ad-5a2367e4008b@app.fastmail.com> In-Reply-To: <1cddd1cb-decf-4381-a290-149cd67bc6ba@php.net> References: <17bf2d84-1744-4879-8120-bd2e15315510@app.fastmail.com> <1cddd1cb-decf-4381-a290-149cd67bc6ba@php.net> Date: Fri, 07 Jun 2024 18:30:18 +0000 To: "php internals" Subject: Re: [PHP-DEV] RFC proposal : "new" keyword shorthand Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") 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 tha= t calls a constructor (kinda sorta like C# extension methods)? Something= kinda like: >>=20 >> class MyClass implements Invocable { >> public function __construct($i) {} >> } >>=20 >> MyClass($i); >>=20 >> Where the Invocable interface defines a function of the same class na= me 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. >>=20 >> I=E2=80=99m not necessarily a fan of magic or symbols, but just tossi= ng it out there to spark ideas.=20 >>=20 >> =E2=80=94 Rob > -- > > I'm not a fan of using ~ for this shorthand, due to the same issues=20 > that have been brought up elsewhere (i.e. it already has meaning). But=20 > the idea of a new interface auto-defining a function of the same name=20 > is attractive. It's something I've done manually to mirror code in PHP=20 > from other implementations existing in Scala leveraging case classes. > > https://docs.scala-lang.org/tour/case-classes.html for anyone=20 > 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`=20 > keyword. Which is to say, I both agree in principle with the RFC and=20 > think this would add value (particularly if I could extend the default=20 > behavior of this kind of interface to support immutability, comparison= ,=20 > etc similar to Scala case classes). I'm just not a fan of the current = ~=20 > proposal. > > ~Eric The issue with auto-generating a function constructor for a class is tha= t 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 fa= tal. =20 If we had function autoloading, that would work, assuming a reasonable g= eneric 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 fo= r a class so autoload it like a class", that could then trigger the clas= s 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 ha= s, sadly, run out of unused symbols. (I've run into this a couple of ti= mes now.) --Larry Garfield