Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120012 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 49339 invoked from network); 13 Apr 2023 06:30:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Apr 2023 06:30:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2753C1804A9 for ; Wed, 12 Apr 2023 23:30:35 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 12 Apr 2023 23:30:34 -0700 (PDT) Received: by mail-oi1-f179.google.com with SMTP id bx42so25758375oib.6 for ; Wed, 12 Apr 2023 23:30:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681367434; x=1683959434; 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=AmqIINLYRfZMpzPNwFca9CdfuNzK/n1YQKQDXGag74o=; b=S+XyS29fgaAcctjvXX41mKwW6/XWjbLfPaLfUBxsLuZNZOA4kEIb2IqtjT/6Ggh7Ai +VKzPE6nLHMxnAKcfcYlSiteZWg7brWTCyAt1epVv8OhuTTk5SeY9Yjg4fFFdgnAQ7gk qYxig4sN+viFmHcLmDq431d5z8eJyinJy2tv4oCypPS3RwKcUt1AEkVghx5mEEB5CHn1 ZT5PolOVIkC9vWuOHdm2WeTNQOLgQjC0W+xXhiP08FyJwNh+yxsPKRaD7YFTKyNLHbhj faRxEC5LTuZ0YBlPxXcOp3K1/1O9MeQ3nLEGDYhgUgO1RS6kLb/8umREQYSlVNmPkOGs MckQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681367434; x=1683959434; 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=AmqIINLYRfZMpzPNwFca9CdfuNzK/n1YQKQDXGag74o=; b=P1xCkDpOZBuPJBThe7sTQV3K8hla6Ce+1mOrzi+cnEJf8TwJv6+kCn2Hs6d7Q8pCZo G4hrcoiFxGCkD2QPU5ZFOAB/7xs502jwCFJG7KShwJhSS7ayUt4TP5eoYQELYYsJxUVx slvi0zod6laGVD6iUeiJUe7hhX9BgyOTXN+FF3zNqhh9WxYrXBOIpLWbSzGezIkzX97E fkL9yfUPdoJW1hUb3x4dssYaeie2P6bTNO5gm/sbTnJr+lTXuQeT8awxE905szoUtWh8 +lHg0pzkuHf0iy8Fzzy85fxI0m7ZKGaDfUaPUJd4rj+yYWjPqx1yKmUQqSreC410EBUD Q2IQ== X-Gm-Message-State: AAQBX9dq52dRUrag9Sh8T5NrLpkhMUfmD9NT3lNWNfNJoahIETHh10qC yGWbVSInKX6mqGb3w3UClqwhDSuEZyoG2PqFs0cpre8so9/YOA== X-Google-Smtp-Source: AKy350aFLrY9ajl4WZYhd13lapDmDY+mLfQ+dgCV8PI5FfJgzF+T5f9lg88W2/ZNyxUBTunv3sO3vAJ+++87bMr1H/Y= X-Received: by 2002:a05:6808:1ce:b0:389:86c3:b1fb with SMTP id x14-20020a05680801ce00b0038986c3b1fbmr252261oic.9.1681367433782; Wed, 12 Apr 2023 23:30:33 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 13 Apr 2023 08:30:19 +0200 Message-ID: To: Eugene Sidelnyk Cc: PHP Internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] First class callable syntax for instance methods From: landers.robert@gmail.com (Robert Landers) On Thu, Apr 13, 2023 at 6:00=E2=80=AFAM Eugene Sidelnyk wrote: > > Hello, internals! I just want to share some thoughts with you regarding > what could be improved in the first class callable syntax. > > It is already possible to create Callable from a static method: > > ``` > class Foo { > public static function staticmethod() {} > } > > $c =3D Foo::staticmethod(...); > ``` > > It would be a great pleasure to have the ability to wrap the instance > method the same way. One particular use case for this could be when there= 's > an array of items and it is necessary to call some getter-method on all o= f > them. > > ``` > class Verification > { > public function __construct(private string $id) {} > > public function getId(): string > { > return $this->id; > } > } > > $items =3D [new Verification('1-2-3-4')]; > > array_map(Verification::getId(...), $items); > > // previous line is an equivalent of this > array_map(static fn (Verification $v) =3D> $v->getId(), $items); > ``` > > Currently this syntax fails in run-time rather than compile-time. > > ``` > PHP Fatal error: Uncaught Error: Non-static method Verification::getId() > cannot be called statically in /opt/scratches/scratch_439.php:21 > ``` > > Please, let me know what you think about it. > > Best regards, Yeven Hello, This has been brought up a couple of times, but I can't seem to find it. I don't think something like this is possible with the current implementation of first-class-callables (it would need a major refactor). Currently adding the callable bit like `func(...)` is roughly the same as just writing a string `'func'` and passing that around (IIRC, that's literally what it gets turned to after parsing). The only difference over using a string, is that there is some validation to check that it is actually a function you can call. Basically, there's currently no way to do something like what you suggest in the engine, which makes it quite a big feature. That being said, we shouldn't be afraid of doing hard things, but it would be a lot of work for saving just a few characters of typing every now and then. Perhaps it would be easier after partial application could be implemented (latest discussion here: https://externals.io/message/119678#119678), or maybe someone else has some better ideas or thoughts. Cheers, Rob Landers Utrecht, Netherlands