Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119407 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 87800 invoked from network); 23 Jan 2023 20:51:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Jan 2023 20:51:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E349518037F for ; Mon, 23 Jan 2023 12:51:01 -0800 (PST) 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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (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 ; Mon, 23 Jan 2023 12:51:01 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id C1E0A5C0166 for ; Mon, 23 Jan 2023 15:51:00 -0500 (EST) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Mon, 23 Jan 2023 15:51:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1674507060; x= 1674593460; bh=6lVksGdfOJDzwAG6PMLLVhBMK6si1NLWHjOkkSyVTLg=; b=V /iRe+22UbEhdOqbcL1VFtWG8xD+GUHFcKVPvH91F3+F91kJU97HuQ/aR+eRGF+7n erRBCVJjyZUGABbkqbchvQCkBJQNZ/vB6DORkIgf8TWyXVBG7YnrhqDCHhG7io1F ZSuDJqm+sYzDZCSBKEWdZReV/ZzwHY3cUhdk9Qol+kbMdPi7aMjQIkJ9hhsSGSFX Z6QjCq18dSlFA+3w5ospj5zK1dLXgKRi0WLFmwIUqvB7sBjHg1mzSlBQtaBc2Jq4 TqFowfCu90RGAMlvNCA/9zfsysyTKo9aSfLSqWunEXJBYsth26QKxuHNGzbNLoso LtsOXYd+f86qUbDshvYpw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1674507060; x=1674593460; bh=6lVksGdfOJDzwAG6PMLLVhBMK6si 1NLWHjOkkSyVTLg=; b=RoAaA8va3iqGq/gPqyfSIQ2yq7NoLo2xJk/NJ1XE2oQa sLpOQUwlAiJNHHjiFzQrBW2FRHgKSc4Kmxr2CwXkUtgxmXdtEfg6Sc/zY2Mh3hqL NdT+P1l2dp0+OmDDtzXEgjg7t60siV566rFAbfPpO+eai/ftpsJ2fqTOAk6G6fTV wir7S1CTbd8KJJvH/B4epiGV0UABqVMUtqsicsAPXalz2648ehsQHvCwgeH4qUhU 3iS0cf+U9hBS7GtCwT/k8CZIWeF+JAdEq4cv8WpVBwLjglqu95MsSWcmGuIgn65b 43mOprB31DlwbkMmoZEIR/L3rLjyKY2YVoHVMmh2DA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedruddukedgudeftdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepjedthffhgedtudehueejjeeivefhvdeugeefffei leegfeeuhedvvdejueevuddtnecuffhomhgrihhnpehgihhthhhusgdrtghomhdpfehvge hlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 70B6E1700089; Mon, 23 Jan 2023 15:51:00 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-85-gd6d859e0cf-fm-20230116.001-gd6d859e0 Mime-Version: 1.0 Message-ID: <9c97b0b1-e17e-4eec-8cf5-fad86aca2da6@app.fastmail.com> In-Reply-To: References: <8f530a4a-bf48-4259-877e-16ebf8082936@app.fastmail.com> Date: Mon, 23 Jan 2023 14:50:39 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Introduce the abiltiy to use the first-call-callable syntax on non-static methods, statically From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jan 23, 2023, at 12:32 PM, Dan Ackroyd wrote: > On Sun, 22 Jan 2023 at 17:45, Ollie Read wrote: >> >> Hello all, > > Hi Ollie, > >> I've created a feature request issue on GitHub (here: https://github.com/php/php-src/issues/10414), but I have been advised that it's best to post here. >> ... >> I think we could delay the error until the closure was called. > > That sounds like a complete non-starter. Adding something to the > language that produces a closure that can't be called would be an > instant new entry for PHPSadness. > >> Whereas it would be much nicer to have the following: >> >> ``` >> $collection->filter(Str::empty(...)); >> ``` >> >> In this situation, the collection library would be responsible for binding the >> closure to the value it is iterating. > > How would the collection library know it was safe to bind the closure > to each value it was iterating over? It sounds really type unsafe. > > >> What I would like to introduce/suggest, is the ability to create a closure >> from a method using the first-class-callable syntax... >> >> The more I think about it, the more I think this may require a new ... > > I think people should say clearly what the problem they are trying to > solve first, before suggesting solutions. > > I think there's at least a couple of problems that can be thought about: > > i. Although the first-class-callable syntax allowed avoiding string > based programming for most callables, it doesn't support referring to > some things that you would want to call, including both instance > methods, and constructors*. > > ii. Representing an instance method requires at least two pieces of > info; which class it belongs to (which can be used to find the > constructor) and the parameters of the method itself. That doesn't > naturally fit into a closure. > > iii. The appropriate syntax for referencing a class instance method > without it being string based isn't obvious. Or at least it isn't > obvious to me. > > iv. Writing code that for callback methods is longer than it could be, > e.g. as Larry Garfield wrote: > >> So what you're really looking for is a shorter way to write this: >> >> foo(fn(Str $s) => $s->beep()); > > > At the risk of suggesting an abomination, given a class of: > > class Zoq > { > public function __construct(private Fot $fot) {} > > public function Pik(string $zebranky): Frungy {...} > } > > If it was possible to generate callable for the constructor with: > > $fnConstructor = Closure::fromClassConstructor(Zoq::class); > // signature of $fnConstructor is the same as `function(Fot $fot): Zoq` > > Or for individual methods: > > $fnMethod = Closure::fromClassMethod(Zoq::class, 'Pik'); > // signature of $fnMethod is the same as `function(Zoq $zoq, string > $zebranky): Frungy` > > That sort of looks like a solution to most of the problems I think exist. > > For your particular problem, I believe that would allow: > > $fnMethod = Closure::fromClassMethod(Str::class, 'empty'); > $collection->filter($fnMethod); > > Which isn't shorter, but at least allows passing the callables around > with the type inspectable. Though it might be nicer if PHP had the > ability to definte function signatures types. > > That sort of solution obviously doesn't address the problem of having > to refer to the class method as a string, but as I said, I don't have > a suggestion for that. Well, other than to use a different syntax.** I don't think that actually helps. If you have the object already, then making an FCC from it already works: https://3v4l.org/GTAGR If you do not have the object already, then we run back into the issue I pointed out before where we need to differentiate a bindable from needs-binding callable, which is already an issue (though one I've not run into). I think that's a prerequisite for adding any dedicated syntax for "make a closure that doesn't work until it gets bound". --Larry Garfield