Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123544 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 1F92E1A009C for ; Fri, 7 Jun 2024 19:03:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717787051; bh=PrUeqcJvL1JJL+yX4dnYTZugzMHaHX9OTlIHHqLmSf0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=I7/21i2lZIzR4YVRn9xmlxsQaCkDIlWcgn0COArPJ3tWhomioWP18h1qfXb62h+b/ cZG8S7CbLN+0rf6I7pMvkgizar1gYs1SyJBFAooA4AhYgCZRyiFpb+vsmgHhGVmmYR VWIBF++janAUTSML+nPMEpL6Vlw5iVZdCM7QhHKrma0qzLLLeF6GXPyv+z1Q60VHhu TSqrpdqKeA2qwuaO4vCS9d39MpDWxPJsXxGs0eF34DN5cVAxQabnVulHk5J8FhSLUi U/Y3EJL/24g8hXlDJwiGo1mOT1kXKMVzoocUg9PcgPAMyOAf7IdI8EsejH8x6eZ9o6 pMSlsvH74/QWw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B93C3180729 for ; Fri, 7 Jun 2024 19:04:10 +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-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (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:04:10 +0000 (UTC) Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-62a0849f8e5so25796387b3.2 for ; Fri, 07 Jun 2024 12:03:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1717786983; x=1718391783; 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=v+XUidFnPGHq/l/ux4c8+wdBr+bb6rrUNUjsr47sGog=; b=AL1Y+FWd/Eq0oJgUBnK7fBlKzgiU2LeJJVdMQkT/I/v2dI4R70oE3TgaStyE/9vAhR CH/q6zgwJl0xpigB8jfpU1rBsd4lTF3k0Xf87dw+2tuFx1rB9BStPMZEoycyJdULuWFW 0PyLOx9sG9AzEXvUIh8nRbhQZ0xGtBJ1XWbXvYRexu5/ZXsfaDbdBMXOBBYW3EFk22ve +6vVhT8goJ1fub1PUrqe86oeS1AwhXYT2q+70al/hPWXxJMq8WzPNwjQYRufGHUgXkfj BGY6rMf9Dt7CDRLZ/lyVX4uckDvomQvb3bGWg1AMdBwlKvoJyBoITj/r663rERvxDl6x TkGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717786983; x=1718391783; 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=v+XUidFnPGHq/l/ux4c8+wdBr+bb6rrUNUjsr47sGog=; b=dUpNWYese9t2hlb036PfW1vXyGVEhfoKITvnu4gpxgqkAYOZ0kYaJ5y7nXzuOqIliH 0agJv5ZrfqvEgAAxY2EdFYYqr7QjZ97acSSV4tpVTZNlPqB1n7vsnBAXsU01oDPPEkGe 3YJBI5g0xtDQpPCko1KW36O6CNggMyhWdSL7v+aHof5/9IcfKUbukt8Qn8tyF1uk48Yh mzN5RdfvCKH/QkZBENN0hwUHJ60yFSmOv9zB1yl+kgZC1AMSI6Is4ztpypLXA3VhZrHj oc0PzLKqp2/dCh+OSUpVxkCWvfaxTxOabU5sMhCkVdyVxznXObEUyQyqjP8f7ibWUQrm 45Og== X-Gm-Message-State: AOJu0Yyp8ifdUCdNxJ1Jc/ELSFF5rbvcZhOmeH3XBOniz4dE5wuCL8sI udIfewZyC7sMD/B1K1zs4yV27AqxYdDl10Scmwlh5TCh2x0GEKjRB+OsvulzMrVr6uYo9DKGNgd U/p1a4RpxLCK9jcsU/ZGgRxAwWZ0DEqUn5nZzRnOke3B0A07sjfvBWQ== X-Google-Smtp-Source: AGHT+IHx3cDjpSonQIVAIOYZl7mdf2BJWnJ2S/zipJMzSAUvlvdCopw1XXgIUbQ71LIPUvPuBR93RolwsT3eHh5LsXA= X-Received: by 2002:a5b:40d:0:b0:dfb:b3e:3815 with SMTP id 3f1490d57ef6-dfb0b3e4485mr665695276.46.1717786969918; Fri, 07 Jun 2024 12:02: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 21:02:39 +0200 Message-ID: Subject: Re: [PHP-DEV] RFC proposal : "new" keyword shorthand To: Larry Garfield Cc: 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 20:31, Larry Garfield wrote: > > 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 call= s a constructor (kinda sorta like C# extension methods)? Something kinda li= ke: > >> > >> 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 fat= al. > > If we had function autoloading, that would work, assuming a reasonable ge= neric autoloader for that case could be written. Who was it that's been ta= lking 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 au= toloader. 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 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