Re: [RFC] Short-function syntax
| From: | tyson andre | Date: | Wed, 21 Oct 2020 22:10:49 +0000 |
| Subject: | Re: [RFC] Short-function syntax | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112097@lists.php.net to get a copy of this message | ||
Hi Larry Garfield,
> > A while back, Nikita mentioned that it should now be easy to offer an
> > abbreviated syntax for functions that are just a single expression. I
> > decided to take a crack at it and it turns out he was right. I thus
> > offer this RFC:
> >
> > https://wiki.php.net/rfc/short-functions
> >
> > Hopefully I made a decent enough case for it. It's entirely a
> > convenience factor, but I think for many OOP cases (getter methods and
> > factored out operations) and functional cases (where functions should
> > generally be a single expression conceptually) it does make the code
> > nicer, more compact, and more readable.
> >
> > *dons flame retardant suit*
>
> There's been some discussion on the PR that I'm looping back here for completeness,
> mainly about using "function" vs "fn" for short named functions.
>
> I can see good arguments for either, and in the end don't care all that much. My main
> concern was that
function was easier to implement, but Sara
> graciously offered an alternate patch that uses fn instead.
> I'll make a PR out of that sometime later today. If we feel it's highly bikeshedable
> I'm happy to make that a secondary vote, or if a clear consensus emerges either way before then
> we can go with whatever that is.
>
> I think I moderately prefer function for visual consistency,
> especially inside a class, but it's not a hill I feel like being moderately injured on. :-)
The feature overall looks convenient.
I'd also be against fn for the consistency - as the RFC says, "Functions are
simpler than lambdas, as there is no need for closing over variables contextually"
The major difference between fn and function() {} is the fact that the
former automatically uses variables by value from the outer scope,
and it'd be confusing to reuse that for methods and/or global functions that don't use
variables for the outer scope.
(I don't know if the RFC mentioned this explicitly)
(e.g. $multiple = 3; fn named_multiply($x) => $x * $multiple; is what a user may
expect fn to mean)
I guess that was mentioned in the PR discussion already, but mentioning this on-list for others: https://github.com/php/php-src/pull/6221#issuecomment-713605293
- Unrelated to this RFC: If we were ever to have class methods and named global functions that did
take variables from the outer scope by reference (like in Python), that may cause complications in
opcache (e.g. for anonymous classes),
and I don't really have a use case for that. But fn would make sense for that
type of feature.
-- Tyson Andre