Re: RFC: Support Closures in constant expressions
| From: | Larry Garfield | Date: | Mon, 04 Nov 2024 20:32:11 +0000 |
| Subject: | Re: RFC: Support Closures in constant expressions | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125908@lists.php.net to get a copy of this message | ||
On Mon, Nov 4, 2024, at 6:06 AM, Tim Düsterhus wrote:
> Hi
>
> Am 2024-10-31 07:16, schrieb Larry Garfield:
>> Hm. It would never occur to me to use a function for a non-class
>> constant in the first place, so I don't know. :-) Frankly I can't
>> recall the last time I used a non-class constant period. :-)
>>
>> That said, PHP consts are already a bit squishy, and auto-capture is
>> always by value. That wouldn't give us a loophole?
>
> Here's another brainteaser:
>
> function foo(
> string $bar,
> Closure $baz = static fn () => $bar,
> ) {
> var_dump($baz());
> }
>
> foo('captured');
>
> What would you expect the semantics of that script to be?
My expectation is that it's confusing, and therefore we should simply disallow it. Which
roughly translates to detecting if a closure tries to use an unbound variable statically. Which is
probably more difficult than I make it sound. :-)
>> If it cannot reasonably be done now, but is possible, that should be
>> listed in future scope with roughly what it would take for a follow-up
>> to do. (And then we can argue if it should just be done now, with more
>> information as to how much work that is.)
>
> I have added an entry to the “Future Scope” section. See also my reply
> to Alex.
>
> Best regards
> Tim Düsterhus
Thanks. Can you include what the implementation challenges are for the future-scope items, to
explain why they're being punted to later rather than addressed now? (As there seems clear
interest in having all of them.)
--Larry Garfield