Re: Idea for better function callbacks (another syntactic sugar)
| From: | Larry Garfield | Date: | Fri, 26 Oct 2018 15:42:47 +0000 |
| Subject: | Re: Idea for better function callbacks (another syntactic sugar) | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-103391@lists.php.net to get a copy of this message | ||
On Friday, October 26, 2018 7:29:46 AM CDT Crocodile wrote:
> Hi internals!
>
> I have this idea of improving the way to specify callbacks for good old PHP
> functions. For instance, I have this piece of code:
>
> ---------------
> array_filter($names, 'trim')
> ---------------
>
> The callback function name is specified as a string, which makes it
> not-so-obvious, although this is definitely a PHP way. An alternative would
> be to rewrite this using a lambda:
>
> ---------------
> array_filter($names, function($name) { return trim($name); })
> ---------------
>
> This is way more wordy, and I bet most of us will go for the first option.
>
> What if we had a more clear way of specifying those callbacks? I suggest
> the following:
>
> ---------------
> array_filter($names, function::trim)
> ---------------
>
> It is, I believe, more clear then a simple string, just a bit more wordy,
> and since "function" is a reserved word which never had anything to do with
> "::", the lexer/parser could probably find a way to deal with this kind of
> syntax (well, honestly, this part is totally unclear for me because I only
> work with PHP from userland).
>
> Does anyone else find this could be a good addition? Or is it not worth
> considering? Or maybe I am missing some obvious pitfalls?
>
> Cheers,
> Victor
I believe the proposal for short lambas (which should get resurrected at some
point) would handle this case well enough as well as help a dozen other
things. To wit:
array_filter($names, |$x| ==> trim($x))
--Larry Garfield
Attachment: [application/pgp-signature] This is a digitally signed message part. signature.asc
Attachment: [application/pgp-signature] This is a digitally signed message part. signature.asc