Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87963 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75912 invoked from network); 31 Aug 2015 21:31:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Aug 2015 21:31:59 -0000 Authentication-Results: pb1.pair.com header.from=inefedor@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=inefedor@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.52 as permitted sender) X-PHP-List-Original-Sender: inefedor@gmail.com X-Host-Fingerprint: 209.85.215.52 mail-la0-f52.google.com Received: from [209.85.215.52] ([209.85.215.52:33404] helo=mail-la0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5A/05-39890-EC7C4E55 for ; Mon, 31 Aug 2015 17:31:58 -0400 Received: by laboe4 with SMTP id oe4so53934716lab.0 for ; Mon, 31 Aug 2015 14:31:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:to:subject:references:date:mime-version :content-transfer-encoding:from:message-id:in-reply-to:user-agent; bh=v/LtTRRmQmQ18H6dEu3HO/zEOnCR4jyqYTC+/TpzzDg=; b=Ewp97ZsZIO4NohQJKjimLVH7aFDep1EQ4pJajciEFfXq+k1Hje0yGr9d6s1C3M0fcE BChH013DTavjqIq/DNk/NYzQfLTUxE6Sy4Wawizx+kTpfN2jwd8zLh4zg4B1DxLZhAkL Fm8MWRCXNyHQjjXEJjRFeB39vnSnykg8kWL+KYuCk6BecLkH5qmKz2LpMq7y/MkOS7G2 IYN8dfsk3YKdiAcKoRjzaMdwzE+3Feo5fHAK2Zsc8qwG6LquPdSZxAJUXCn7cVJr0V7q dn9pHk+HFiLeLNg9Iau6YW8cgNSQ0nZEjKjVLXaXYWvm2SVt26NEMfo9jIjkCz0VBdzM Ph2Q== X-Received: by 10.112.182.42 with SMTP id eb10mr11254471lbc.62.1441056715515; Mon, 31 Aug 2015 14:31:55 -0700 (PDT) Received: from nikita-pc ([89.250.5.98]) by smtp.gmail.com with ESMTPSA id j1sm4257999lbc.15.2015.08.31.14.31.54 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 31 Aug 2015 14:31:54 -0700 (PDT) Content-Type: text/plain; charset=koi8-r; format=flowed; delsp=yes To: internals@lists.php.net References: <55E4C19F.4060704@gmail.com> Date: Tue, 01 Sep 2015 00:32:07 +0300 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <55E4C19F.4060704@gmail.com> User-Agent: Opera Mail/12.17 (Win32) Subject: Re: [PHP-DEV] [RFC] [Discussion] Short Closures From: inefedor@gmail.com ("Nikita Nefedov") On Tue, 01 Sep 2015 00:05:35 +0300, Stanislav Malyshev wrote: > Hi! > >> I had this RFC in draft since some time, but delayed it due to all >> the ongoing PHP 7 discussions. Also we have no master branch to merge >> features in until 5.4 EOL. Thus I'm reviving this now. >> >> Time for the first RFC targeting PHP 7.1 (assuming PHP 8 isn't going >> to be the next version ;-)): >> >> The short Closures RFC: https://wiki.php.net/rfc/short_closures > > I personally don't see much advantage in making PHP code less readable > and harder to parse. Also, "automatically use () all of the (compiled) > variables" does not look like a good idea - first, users have no idea > what compiled variables are, so the result would be unpredictable for > them, second, if there are a lot of variables in the scope (like global > scope) it would have huge performance costs for no gain. > Also, this syntax does not allow "use by-ref" which the existing syntax > does allow. > > I think we already have syntax for anonymous functions and it works. The > only addition in this RFC is automatically gobbling up all local > variables, which in vast majority of use cases is not what the function > needs. Hi Stas, Capture of all variables from the current scope is not the only addition in this RFC. The main addition (which is extended by that btw) is making the syntax shorter. Which, in turn, makes it easier to write and read closures. I've had a huge lot of cases where I would benefit from it, things like this: $collection = array_filter(array_map(function (Type $el) { return $el->getStuff(); }, $collection)); It would be much easier to read and write with the short syntax: $collection = array_filter(array_map((Type $el) ~> $el->getStuff(), $collection)); Just because, I'll say it, the less symbols you have to read the faster you'll read it. To some extent, of course, a counter-example being Perl language. But I don't think that we are even close to it. Your point about capturing too much if you have too much in the global scope is legit but in this case I'd say two things: you can still use long closure syntax, and second you have a problem with global scope not short closures. You see the main use case for anon. functions is when you need a *lightweight* callback for something. If it were a fat function I'd probably make it named and write tests for it. But when it's a small function, often consisting from only one expression it kinda bothers me when the body of this function takes less keystrokes than 'function' and 'return' keywords...