Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88612 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32131 invoked from network); 1 Oct 2015 17:10:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Oct 2015 17:10:48 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.170 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.170 mail-wi0-f170.google.com Received: from [209.85.212.170] ([209.85.212.170:33230] helo=mail-wi0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 09/11-26330-6196D065 for ; Thu, 01 Oct 2015 13:10:48 -0400 Received: by wiclk2 with SMTP id lk2so251518wic.0 for ; Thu, 01 Oct 2015 10:10:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type; bh=gFfXxncTHRie2+wvLJXxiwLKnJQPkV4NZ8R65zUlaBU=; b=t8K6M9FrIrhrCYEYZnzrtK+8jYEOCwQ6IHDK9pTks51XlJGsnS1VlMnOYx9Hf/EqAr UkwgmkZDIlAu/HHxY0Gmm6cjQ2r0k9oRsAgwEEgAZqeOFncskjopPjK04KUF2ZPhW4fw o/JTcmMH09lMORfyagDZhJFsq/ELwKK5k45s0XTGLgkA2ZlftRnuubQtdm9KwRbNndRZ MKFV3byttsl+6OeB+UWdiyLUdKnzIDIm4Q/1cGKevuKGspDx6nZj/DMTmjMSyT+KYrCb TTtsNA9dQoawv0ttRjjTtrIGaxngSVrsC/JjQjT6jtzvB2S1yKIakr1jMfD3FCuZLuM3 4irw== X-Received: by 10.180.205.234 with SMTP id lj10mr4237330wic.49.1443719443355; Thu, 01 Oct 2015 10:10:43 -0700 (PDT) Received: from [192.168.0.161] ([62.189.198.114]) by smtp.googlemail.com with ESMTPSA id xt1sm7066024wjb.32.2015.10.01.10.10.42 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 01 Oct 2015 10:10:42 -0700 (PDT) To: Nikita Nefedov , internals@lists.php.net References: <5606D0EB.3060106@gmail.com> <560D0F94.8060009@gmail.com> <560D282F.6060202@gmail.com> Message-ID: <560D690C.8050606@gmail.com> Date: Thu, 1 Oct 2015 18:10:36 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------000501080406040500010004" Subject: Re: [PHP-DEV] Arrow function expressions in PHP From: rowan.collins@gmail.com (Rowan Collins) --------------000501080406040500010004 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Nikita Nefedov wrote on 01/10/2015 15:58: > On Thu, 01 Oct 2015 15:33:51 +0300, Rowan Collins > wrote: >> It is a tool for making them shorter, yes, but it is not the only >> way, and it comes with a cost of changing established behaviour. >> Again, look at C++'s lambda syntax - it is much shorter than PHP's, >> but requires a list of all variables from the outer scope being >> captured. > > C++11 doesn't *require* the list of all variables, but it does require > explicit "wildcard" ([=] for copy semantics, [&] for capturing by > reference). I'm not actually that familiar with C++'s lambdas, and was basing my understanding on this page: https://msdn.microsoft.com/en-us/library/dd293608.aspx I read this line as implying the same behaviour as PHP's: > An empty capture clause, *[ ]*, indicates that the body of the lambda expression accesses no variables in the enclosing scope. But on closer inspection, the meaning of [=] on its own is just not terribly clear on that particular description, so I stand corrected. > But C++ is not the best place for picking up syntax choices, quite > frankly it's one of the least readable languages in a way that it > allows you to write very cryptic code easily (but still allows to > write perfectly readable code as well). Sure, short syntax leads to hard-to-read code. A good argument to stick to the existing PHP verbosity. And, again, I was not advocating C++'s exact syntax (it conflicts with existing syntax anyway), merely showing that "short" and "explicitly listed captures" are not mutually exclusive. > I don't think there was a dozen of different ideas, I could only find > those about `lambda(arg-list; use-list; expression)` and variations of > it with different keywords and different return-type syntax. Fair enough. But there's no point me addressing bikeshedding details about semi-colon vs other punctuation, etc, etc, if you are fundamentally opposed to requiring an explicit list of used variables. > And one thing that makes auto capture much better choice than explicit > capture (as it've been said a couple of times already) is partial > application: > > $mul = fn($x) => fn($y) => fn($z) => $x * $y * $z; > > Looks simpler than: > > $mul = fn($x) => fn($y; $x) => fn($z; $x, $y) => $x * $y * $z; It looks simpler, but it's an illusion - there is actually a closure hidden in there that has argument ($z) and captures ($x, $y). To someone not used to chaining, and not expecting scopes to be inherited, it's far from simple to follow where $x and $y come from and end up. Let's pick another syntax - again, completely off the top of my head, may not even be possible: # { args|captures => return } $double = { $x => $x * 2 } $mul = {$x => {$y|$x => {$z|$x,$y => $x * $y * $z} } } Note that the nesting is indicated explicitly, rather than via associativity, the captured variables are listed explicitly, but clearly separated from the argument list. So my question is: if a short syntax was suggested which required explicit capture but otherwise seemed compact and elegant, would you be interested, or do you consider auto-capture a non-negotiable feature? Regards, -- Rowan Collins [IMSoP] --------------000501080406040500010004--