Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87973 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16380 invoked from network); 1 Sep 2015 05:53:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Sep 2015 05:53:45 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.171 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.217.171 mail-lb0-f171.google.com Received: from [209.85.217.171] ([209.85.217.171:33282] helo=mail-lb0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 73/6A-39890-86D35E55 for ; Tue, 01 Sep 2015 01:53:44 -0400 Received: by lbbsx3 with SMTP id sx3so72584323lbb.0 for ; Mon, 31 Aug 2015 22:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=hrobpeVjpCZtJLXqnahpM5d/Z3nW652RavQQeh45FKc=; b=tKjpMp3wAIS60wkBKYWnG7SE0IaCvflu+Mlb7PI9YCDtdp0IVM4d+r/7wGRNbRJFWA rUhM8aa0xTHhgyK/vg4NiUtZ5fKIGQ22+xL0d7OG2c3nSz61DVpRh5IXNziqRe15Ebdv iWXrRBgIHaXZJNaa0XiISGBsTB+HS/441KBM+UXIMuZJkam81/prGK1U9BqyaqZzCy8D NQIF241oQmzGfyqI/vSgZu17zq3cEoPOwQVpeHFQFZQyjXWKsa4X12tfgymKumy93eua X3hSM3QWIx0BNPl6WSm8NqCddeK+EDHdpuS8C3iBFXeKKmmmkNFTjwpe0Zz7DQyEoc/Z Tw6g== MIME-Version: 1.0 X-Received: by 10.152.179.107 with SMTP id df11mr11812166lac.95.1441086821292; Mon, 31 Aug 2015 22:53:41 -0700 (PDT) Received: by 10.25.141.131 with HTTP; Mon, 31 Aug 2015 22:53:41 -0700 (PDT) In-Reply-To: <55E51FAA.8030106@gmail.com> References: <55E4C19F.4060704@gmail.com> <55E4F029.10104@gmail.com> <7BCB36EE-56C8-441F-BF40-954D9FA912A9@lerdorf.com> <55E51FAA.8030106@gmail.com> Date: Tue, 1 Sep 2015 01:53:41 -0400 Message-ID: To: Stanislav Malyshev Cc: Bob Weinand , "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [RFC] [Discussion] Short Closures From: ircmaxell@gmail.com (Anthony Ferrara) Stas, On Mon, Aug 31, 2015 at 11:46 PM, Stanislav Malyshev wrote: > Hi! > >> Here it is very obvious that we want to import a variable. Especially, I wonder how >> $array = array_map(function ($x) use ($y) { return $x + $y; }, $array); >> is making such simple applications more readable? And especially, what value does it add here? > > It is making it more readable by explicitly specifying that we're using > function, that this function imports $y from parent scope and returns $x > + $y. Of course, a seasoned programmer that has years of experience in > functional languages would recognize this pattern, but that's not > exactly the main target audience of PHP. I think that's quite a bit hyperbolic of an argument. Most programming languages today have a "short form" closure or lambda syntax HackLang: ($x) ==> $x + 1; C++: [](int x) -> int { return x + 1; } Java: (int x) -> x + 1; Python: lambda x: x+1 Ruby: lambda |x| { x + 1 } Rust: |x| x + 1 JavaScript (ES6): x => x + 1 C#: x => x + 1 Objective C: ^(int x) { return x + 1; } Note that not a single one of those is a "functional" language. That's every language in the top 10 index except PERL, PHP and VB.NET (though VB has a similar syntax: function (x as Integer) x + 1), I agree that at first it will feel a little bit weird, especially given that PHP in general lacks syntactic sugar (we still don't have a short-hand to initialize stdclass objects). We introduced [] back in 5.4, and largely it has made readability FAR better (despite some people saying it wouldn't at the time). > And I do not think the argument "you don't have to use it" is a good one > for adding language syntax constructs. If it's in the language, you'd > have to deal with it, and you'd have to teach other people to deal with > it. It's not some function sitting in the extension that may not even be > loaded - it's part of language syntax. So if it would encourage > write-only code, you'd have to deal with it even if you don't personally > create write-only code. I agree 100%. "you don't have to use it" sucks as an argument. However I don't think that's what Bob meant. He was trying to convey that the tool isn't always appropriate in all situations. You can craft code that makes this new syntax look unreadable. But you can craft code that makes this look far more readable. Example: function partial(callable $cb) { return function($left) use ($cb) { return function($right) use ($cb, $left) { return $cb($left, $right); }; }; } vs: function partial(callable $cb) { return $left ~> $right ~> $cb($left, $right); } It may look weird at first, but consider that's because you're not used to the syntax. To me, the top is far harder to follow because of the shear number of tokens to follow, not to mention that I need to pay attention to the use-blocks which may or may not be importing certain variables. The bottom reads exactly how I would expect it to. Both are completely explicit in what they do and how they behave. That's my opinion anyway... Anthony