Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88517 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 30765 invoked from network); 26 Sep 2015 18:31:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Sep 2015 18:31:52 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.45 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.213.45 mail-vk0-f45.google.com Received: from [209.85.213.45] ([209.85.213.45:33330] helo=mail-vk0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 86/A2-06395-694E6065 for ; Sat, 26 Sep 2015 14:31:50 -0400 Received: by vkgd64 with SMTP id d64so72489547vkg.0 for ; Sat, 26 Sep 2015 11:31:47 -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:content-transfer-encoding; bh=1B9gjHFpLq2P2It19I2pEJQUtFOH9l9vFoDcWGEbSI0=; b=uE9gWA7OyEixmY3r2NCCsteqrjbBSJgLXhpaDv9milQSUMwQEsbcAOH+U5jh1RKArx a64+sfcVI/88hUNpHQKr1600tclXA6kMy1wHJSwBcbQ8hQ+212h22w4jhkaoNNR5j95D ZMrzhcijnWlBuoUb+utAXmJAoCEEioNZlBZOR9J2ozO0wilOkT9X/vogx1ZVTNysJr5Q uHG7SxxPVK2yr4IKazFzE6IpwuP2Kl7pECnT6quuS2YkwOQ5uVhor9/TziO2fREvs6qb lbuFxKF7cBmUIHdnBSo0rmZODO8Kl5nhmPO/t26steiy8tuEtqIDaYQ7DJ+Y7elPFyVF 0JhA== MIME-Version: 1.0 X-Received: by 10.31.174.135 with SMTP id x129mr6624271vke.26.1443292307027; Sat, 26 Sep 2015 11:31:47 -0700 (PDT) Sender: morrison.levi@gmail.com Received: by 10.31.41.205 with HTTP; Sat, 26 Sep 2015 11:31:46 -0700 (PDT) In-Reply-To: <5606D0EB.3060106@gmail.com> References: <5606D0EB.3060106@gmail.com> Date: Sat, 26 Sep 2015 12:31:46 -0600 X-Google-Sender-Auth: u_Tgdq5yXwr4c9OjkFhZXBEo3ZY Message-ID: To: Rowan Collins Cc: internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Arrow function expressions in PHP From: levim@php.net (Levi Morrison) On Sat, Sep 26, 2015 at 11:07 AM, Rowan Collins w= rote: > On 26/09/2015 17:17, Levi Morrison wrote: >> >> What concerns do you have about `fn($x) =3D> $x * 2` or `function($x) = =3D> >> $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 =3D> as a sep= arate > token looks a bit weird. It no longer matches other languages, so how abo= ut > thinking a bit further outside the box? Thank you for the feedback. I feel like the rest of what you proposed was a bit too far outside of the box. For what it is worth no token after the paren is necessary =E2=80=93 you could do `fn($x) $x * 2` (or `function($x) $x * 2`). I think this is a case where more syntax helps. On Sat, Sep 26, 2015 at 11:07 AM, Rowan Collins w= rote: > On 26/09/2015 17:17, Levi Morrison wrote: >> >> What concerns do you have about `fn($x) =3D> $x * 2` or `function($x) = =3D> >> $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 =3D> as a sep= arate > token looks a bit weird. It no longer matches other languages, so how abo= ut > 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 giv= e a > place for bound variables without the "use" keyword. e.g. your example co= uld > become fn($x;; $x * 2) > > I picked "lambda" as the keyword before, and gave these examples: > > # lambda(params; bound vars; expression) > $double =3D lambda($a;; 2*$a) > $x=3D3; $triple =3D lambda($a; $x; $x * $a) > > function sumEventScores($events, $scores) { > $types =3D 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) =3D> { 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) =3D> { foo($x); bar($x, $y); } > > Regards, > > -- > Rowan Collins > [IMSoP] > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php