Re: [RFC] Arrow functions / short closures

From: Date: Wed, 13 Mar 2019 23:01:02 +0000
Subject: Re: [RFC] Arrow functions / short closures
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-104704@lists.php.net to get a copy of this message
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

« previous php.internals (#104704) next »