On 12/06/2022 03:01, Larry Garfield wrote:
... users don't "learn to recognize" that a loop doesn't hide all variables
from the parent
scope; it would be very peculiar if it did.
There are languages that do, however. Some languages have block-scoped
variables by default (such as Rust), or partially blocked scoped depending on
details.
That's not what the RFC example implies, though; it implies that someone
might expect $guests and $guestsIds to not be usable *inside* the
foreach loop, because they were declared *outside* it. I don't know of
any language where entering a loop creates a completely empty symbol
table, do you?
Whether or not $guest, having been declared *inside* the loop, is
visible *after* the loop is a completely different question, and one
that doesn't apply to closures - the content of the closure hasn't been
executed yet, so it is inevitably a black box to the code after it.
PHP is fairly internally consistent: functions and classes create a scope,
nothing else does. This RFC doesn't change that one way or another...
The RFC fundamentally changes the rule that a function always creates a
new, *empty* scope. Every variable that is not local to that scope has
to be explicitly imported, one way or another.
Every language I know where scopes do *not* start out empty has keywords
for marking which variables are definitely local to that block. That's
why I think a "var" or "let" equivalent is a natural accompaniment to
changing PHP's rules in this way.
Plus, as noted, the `fn` keyword becomes consistently the flag saying "auto-capture
happens here, FYI", which is already the case as of 7.4.
It's a cute idea, but I don't think "if you miss most of the letters out
of a word, it means this special thing" is at all memorable. I've never
heard the syntax added in 7.4 called "fn functions", but I've frequently
heard it called "arrow functions", because what stands out to people is
the "=>". The keyword is only there because ($a)=>$b on its own would
collide with array syntax.
I would much rather see "fn" and "function" become synonyms, so that
"public fn foo() {}" is a valid method declaration, and "function() =>
$foo" is a valid arrow function.
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php