Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88516 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25766 invoked from network); 26 Sep 2015 17:08:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Sep 2015 17:08:04 -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:36092] helo=mail-wi0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1B/02-06395-3F0D6065 for ; Sat, 26 Sep 2015 13:08:03 -0400 Received: by wicgb1 with SMTP id gb1so55719475wic.1 for ; Sat, 26 Sep 2015 10:07:59 -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:content-transfer-encoding; bh=IU8D168uqRwZQRnYafCQrMIiCvlARtLL9+mG9Gl+2IA=; b=lQAuldV1Qn255solbVv0Z1DJZW8wP01E0rPptNvv12kqgZjwZz6gidMRd2hpe+Drvw 21R1uF2fY3MT6T2R9hQOdH/kgIkgGY0M6o9BbdMRgaK5/UgaaHdTZvSQ4xlNOQxv2zRv Jm4LTQnLxYL/EA2YOdlzz+9/tF7Iszkugt9mjwctrDhX9wb0m6beK6xkwRkZ2XoEmMEq Bp6t/b6tkWY9rzGXWwExGMKVonGMEk0tybC/uc6Zl5jcIc32q4i7n8/BLA6A7N5gaRu3 i2XgF1qyDxvRuohufbBPcUw/33jmR2Dj7C4NVhPXEtJSvENPOmpAfDHtQjtzCvrOejvu KU0Q== X-Received: by 10.180.103.72 with SMTP id fu8mr9612614wib.7.1443287279758; Sat, 26 Sep 2015 10:07:59 -0700 (PDT) Received: from [192.168.0.5] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by smtp.googlemail.com with ESMTPSA id o3sm9097319wif.22.2015.09.26.10.07.58 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 26 Sep 2015 10:07:59 -0700 (PDT) To: internals@lists.php.net References: Message-ID: <5606D0EB.3060106@gmail.com> Date: Sat, 26 Sep 2015 18:07:55 +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: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Arrow function expressions in PHP From: rowan.collins@gmail.com (Rowan Collins) On 26/09/2015 17:17, Levi Morrison wrote: > What concerns do you have about `fn($x) => $x * 2` or `function($x) => > $x * 2`? I will be writing a proper RFC later but I wanted to get > discussion going now. If a keyword is required next to the parameters, having the => as a separate token looks a bit weird. It no longer matches other languages, so how about thinking a bit further outside the box? One of the random thoughts that popped into my head during the previous discussion was to base the syntax on the C for-loop, which would also give a place for bound variables without the "use" keyword. e.g. your example could become fn($x;; $x * 2) I picked "lambda" as the keyword before, and gave these examples: # lambda(params; bound vars; expression) $double = lambda($a;; 2*$a) $x=3; $triple = lambda($a; $x; $x * $a) function sumEventScores($events, $scores) { $types = array_map(lambda($event;; $event['type']), $events); return array_reduce($types, lambda($sum, $type; $scores; $sum + $scores[$type])); } Adding in the type information, we'd get this: lambda(int $sum, string $type; $scores; $sum + $scores[$type]) # or with fn() if you prefer: fn(int $sum, string $type; $scores; $sum + $scores[$type]) If return typehints are also required, I'm not sure where they'd best be placed. If they're outside the parens, they end up after the expression, which might look odd: fn(int $sum, string $type; $scores; $sum + $scores[$type]): int A few other possibilities: fn(int $sum, string $type; $scores; $sum + $scores[$type]; int) fn(int $sum, string $type: int; $scores; $sum + $scores[$type]) fn:int(int $sum, string $type; $scores; $sum + $scores[$type]) All of this assumes that the shorthand is only available for simple expressions, not function bodies, but it seems a bit rendundant to allow both of these: function($x) { foo(); bar(); baz(); } fn($x) => { foo(); bar(); baz(); } And only marginally more useful if variables are auto-captured, with all the downsides of that which have already been raised: function($x) use ($y) { foo($x); bar($x, $y); } fn($x) => { foo($x); bar($x, $y); } Regards, -- Rowan Collins [IMSoP]