Fake Closure Comparison

  115375
July 9, 2021 08:45 krakjoe@php.net (Joe Watkins)
Morning internals,

While discussing some of the details of the first class callable RFC, it
became apparent that fake closures (created by Closure::fromCallable) are
not currently comparable in a useful way.

Although this is not directly related to the first class callable feature,
it's likely that the proliferation of this kind of code will follow, so now
seems like a good time to fix the problem.

https://github.com/php/php-src/pull/7223

Any objections to merging that in master ?

Cheers
Joe
  115376
July 9, 2021 09:51 nikita.ppv@gmail.com (Nikita Popov)
On Fri, Jul 9, 2021 at 10:45 AM Joe Watkins <krakjoe@php.net> wrote:

> Morning internals, > > While discussing some of the details of the first class callable RFC, it > became apparent that fake closures (created by Closure::fromCallable) are > not currently comparable in a useful way. > > Although this is not directly related to the first class callable feature, > it's likely that the proliferation of this kind of code will follow, so now > seems like a good time to fix the problem. > > https://github.com/php/php-src/pull/7223 > > Any objections to merging that in master ? >
To clarify: What this implements is that $strlen1 = Closure::fromCallable('strlen'); $strlen2 = Closure::fromCallable('strlen'); var_dump($strlen1 == $strlen2); // bool(true) with this patch, previously bool(false) The same would also apply to the first-class callable syntax, i.e. after this patch strlen(...) == strlen(...) would return true. Regards, Nikita
  115379
July 9, 2021 10:59 larry@garfieldtech.com ("Larry Garfield")
On Fri, Jul 9, 2021, at 4:51 AM, Nikita Popov wrote:
> On Fri, Jul 9, 2021 at 10:45 AM Joe Watkins <krakjoe@php.net> wrote: > > > Morning internals, > > > > While discussing some of the details of the first class callable RFC, it > > became apparent that fake closures (created by Closure::fromCallable) are > > not currently comparable in a useful way. > > > > Although this is not directly related to the first class callable feature, > > it's likely that the proliferation of this kind of code will follow, so now > > seems like a good time to fix the problem. > > > > https://github.com/php/php-src/pull/7223 > > > > Any objections to merging that in master ? > > > > To clarify: What this implements is that > > $strlen1 = Closure::fromCallable('strlen'); > $strlen2 = Closure::fromCallable('strlen'); > var_dump($strlen1 == $strlen2); > // bool(true) with this patch, previously bool(false) > > The same would also apply to the first-class callable syntax, i.e. after > this patch strlen(...) == strlen(...) would return true. > > Regards, > Nikita
Seems reasonable on the surface. My main question is how far that extends. Does it work *only* for Closure::fromCallable()/FCC syntax? What if the argument is not a string? How deep does the comparison go for, say, objects and methods? class C { public function beep(string $a): string { return $a . 'beep'; } } $c1 = new C(); $c2 = new C(); $strlen1 = Closure::fromCallable([$c1, 'beep']); $strlen1b = Closure::fromCallable([$c1, 'beep']); $strlen2 = Closure::fromCallable([$c2, 'beep']); // What do these do? var_dump($strlen1 == $strlen1b); var_dump($strlen1 == $strlen2); --Larry Garfield
  115380
July 9, 2021 11:07 krakjoe@gmail.com (Joe Watkins)
The test included is quite comprehensive.

Cheers
Joe

On Friday, 9 July 2021, Larry Garfield <larry@garfieldtech.com> wrote:

> On Fri, Jul 9, 2021, at 4:51 AM, Nikita Popov wrote: > > On Fri, Jul 9, 2021 at 10:45 AM Joe Watkins <krakjoe@php.net> wrote: > > > > > Morning internals, > > > > > > While discussing some of the details of the first class callable RFC, > it > > > became apparent that fake closures (created by Closure::fromCallable) > are > > > not currently comparable in a useful way. > > > > > > Although this is not directly related to the first class callable > feature, > > > it's likely that the proliferation of this kind of code will follow, > so now > > > seems like a good time to fix the problem. > > > > > > https://github.com/php/php-src/pull/7223 > > > > > > Any objections to merging that in master ? > > > > > > > To clarify: What this implements is that > > > > $strlen1 = Closure::fromCallable('strlen'); > > $strlen2 = Closure::fromCallable('strlen'); > > var_dump($strlen1 == $strlen2); > > // bool(true) with this patch, previously bool(false) > > > > The same would also apply to the first-class callable syntax, i.e. after > > this patch strlen(...) == strlen(...) would return true. > > > > Regards, > > Nikita > > Seems reasonable on the surface. My main question is how far that > extends. Does it work *only* for Closure::fromCallable()/FCC syntax? What > if the argument is not a string? How deep does the comparison go for, say, > objects and methods? > > class C { > public function beep(string $a): string { return $a . 'beep'; } > } > > $c1 = new C(); > $c2 = new C(); > > $strlen1 = Closure::fromCallable([$c1, 'beep']); > $strlen1b = Closure::fromCallable([$c1, 'beep']); > $strlen2 = Closure::fromCallable([$c2, 'beep']); > > // What do these do? > var_dump($strlen1 == $strlen1b); > var_dump($strlen1 == $strlen2); > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > >
  115377
July 9, 2021 10:08 pierre-php@processus.org (Pierre)
Le 09/07/2021 à 10:45, Joe Watkins a écrit :
> Morning internals, > > While discussing some of the details of the first class callable RFC, it > became apparent that fake closures (created by Closure::fromCallable) are > not currently comparable in a useful way. > > Although this is not directly related to the first class callable feature, > it's likely that the proliferation of this kind of code will follow, so now > seems like a good time to fix the problem. > > https://github.com/php/php-src/pull/7223 > > Any objections to merging that in master ? > > Cheers > Joe
I don't see where I would actually compare callables, and I'm not an engine maintainer, but from my point of view, this patch makes sense. Regards, -- Pierre
  115396
July 12, 2021 07:51 nikita.ppv@gmail.com (Nikita Popov)
On Fri, Jul 9, 2021 at 12:08 PM Pierre <pierre-php@processus.org> wrote:

> Le 09/07/2021 à 10:45, Joe Watkins a écrit : > > Morning internals, > > > > While discussing some of the details of the first class callable RFC, it > > became apparent that fake closures (created by Closure::fromCallable) are > > not currently comparable in a useful way. > > > > Although this is not directly related to the first class callable > feature, > > it's likely that the proliferation of this kind of code will follow, so > now > > seems like a good time to fix the problem. > > > > https://github.com/php/php-src/pull/7223 > > > > Any objections to merging that in master ? > > > > Cheers > > Joe > > I don't see where I would actually compare callables, and I'm not an > engine maintainer, but from my point of view, this patch makes sense. >
I'm also not sure where this would be actually useful (though I'm okay with including it, as the functionality itself seems sensible). As Patrick Allaert suggested this, maybe he can comment on some use cases. Regards, Nikita
  115398
July 12, 2021 09:42 patrickallaert@php.net (Patrick ALLAERT)
Le lun. 12 juil. 2021 à 09:51, Nikita Popov ppv@gmail.com> a écrit :

> I'm also not sure where this would be actually useful (though I'm okay > with including it, as the functionality itself seems sensible). As Patrick > Allaert suggested this, maybe he can comment on some use cases. >
I can't really see a useful case either, it was more rhetorical. For a feeling I can't explain much, I was caring less about: Closure::fromCallable('strlen') == Closure::fromCallable('strlen') than: strlen(...) == strlen(...) I know they are technically identical, but I see the first construct as a function call while the second one looks more like a language construct "referencing" (sorry for not using a more adequate verb) the same function. Anyway, I don't have a strong opinion on it. Regards, Patrick
  115399
July 12, 2021 09:45 patrickallaert@php.net (Patrick ALLAERT)
Le lun. 12 juil. 2021 à 11:42, Patrick ALLAERT <patrickallaert@php.net> a
écrit :

> Le lun. 12 juil. 2021 à 09:51, Nikita Popov ppv@gmail.com> a > écrit : > >> I'm also not sure where this would be actually useful (though I'm okay >> with including it, as the functionality itself seems sensible). As Patrick >> Allaert suggested this, maybe he can comment on some use cases. >> > > I can't really see a useful case either, it was more rhetorical. >
Maybe: if ($f == unlink(...)) { logging("Deleting $x"); } $f($x);