Re: [PHP-DEV] [RFC] Arrow functions / short closures

This is only part of a thread. view whole thread
  104704
March 13, 2019 23:01 bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=)
Den 2019-03-13 kl. 16:56, skrev Nikita Popov:
> Hi internals, > > Motivated by the recent list comprehensions RFC, I think it's time we took > another look at short closures: > > https://wiki.php.net/rfc/arrow_functions_v2 > > This is based on a previous (withdrawn) proposal by Levi & Bob. It uses the > syntax > > fn($x) => $x * $multiplier > > and implicit by-value variable binding. This example is roughly equivalent > to: > > function($x) use($multiplier) { return $x * $multiplier; } > > The RFC contains a detailed discussion of syntax choices and binding modes. > > Regards, > Nikita
Thanks for bringing this forward Nikita! I recall from the earlier discussions 2017 that also the lambda as a keyword was considered. I would like to bring that forward as one option, maybe less impact on existing code as a reserved keyword. An advantage of not having a keyword /prefix could be that for simple arrow functions no parenthesis is needed and readability is improved: $someDict->map(fn($v) => $v * 2)->filter(fn($v) => $v % 3); $someDict->map(\($v) => $v * 2)->filter(\($v) => $v % 3); $someDict->map(^($v) => $v * 2)->filter(^($v) => $v % 3); vs $someDict->map($v ==> $v * 2)->filter($v ==> $v % 3); $someDict->map($v ~> $v * 2)->filter($v ~> $v % 3); r//Björn L