Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88589 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63233 invoked from network); 1 Oct 2015 03:06:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Oct 2015 03:06:52 -0000 Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.42 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.213.42 mail-vk0-f42.google.com Received: from [209.85.213.42] ([209.85.213.42:36793] helo=mail-vk0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6D/B2-38941-A43AC065 for ; Wed, 30 Sep 2015 23:06:51 -0400 Received: by vkfp126 with SMTP id p126so31742151vkf.3 for ; Wed, 30 Sep 2015 20:06:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=jbS4u66KfcSIGowefTUpJ0kOuu6KTRpx/TYDu6veJV4=; b=QgRA+1DFt8N0LytS4iVB5ltcHglnuSfq4WbvmiYkRgEguH7HBzRyz6ajAhChWaTHb4 7i18BHiBYLH9lEkH45/Mvw+nAbhAPqP48bU9qwlvMmJBg5DcbfY0m7hya0UEfFDnHc2B vbdFrZPkr83nFLfrpTVgTnm5PpiAVMprp3w3Xo+F0AV/djFKlVdKKrkD9GUWJzIcXvbB j6Lh7tCoYpuNgmQUMDpmp3WEjPjFMHBLTDciuY6g6xXnZczdCgcFFWwclE8+tGLEd6tA 1CsPBg77jp/bNNZEEP9XyRfSNiIkIT/PuAOp7fJSqa+Dt4muXTq2T1O1R1jCgeZncb9i n8yw== MIME-Version: 1.0 X-Received: by 10.31.163.75 with SMTP id m72mr5364999vke.54.1443668808280; Wed, 30 Sep 2015 20:06:48 -0700 (PDT) Sender: morrison.levi@gmail.com Received: by 10.31.41.205 with HTTP; Wed, 30 Sep 2015 20:06:48 -0700 (PDT) In-Reply-To: References: <5606D0EB.3060106@gmail.com> Date: Wed, 30 Sep 2015 21:06:48 -0600 X-Google-Sender-Auth: krhC2It3XLRszVMUW6bwv3VjKkc Message-ID: To: bishop@php.net Cc: Rowan Collins , PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Arrow function expressions in PHP From: levim@php.net (Levi Morrison) On Wed, Sep 30, 2015 at 8:45 PM, Bishop Bettini wrote: > On Sat, Sep 26, 2015 at 1:07 PM, Rowan Collins > wrote: >> >> 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); } > > > I'm leaning toward a compromise between Levi's suggested syntax (which is > unambiguous and short, but auto-closes over parent's scope) and Rowan's > for-loop style (which imports variables but the syntax feels cramped to me). > Example: > > $a = 1; > $b = fn($x; $a) => $x + $a; // note the semi-colon here, $a is explicitly > imported > $c = $b(1); // 2 I'm going to ask everyone to stop saying that auto-closing is bad unless they also provide a demonstration of why it was bad. I'm also going to ask everyone to stop suggesting new syntax for importing variables. If use() is a pain then auto-importing the used variables is a good solution. If it's not a pain why are you suggesting new syntax?