Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104710 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68789 invoked from network); 14 Mar 2019 12:36:37 -0000 Received: from unknown (HELO mail-io1-f49.google.com) (209.85.166.49) by pb1.pair.com with SMTP; 14 Mar 2019 12:36:37 -0000 Received: by mail-io1-f49.google.com with SMTP id v4so3389395ioj.5 for ; Thu, 14 Mar 2019 02:26:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=BmiQFSzRLq7R6UY1Tbv/pcPYh/URV7xmTZN8bD5iKlE=; b=HGAtoZWUD/aj6ShvXmgoN8mgELbpeq3CV+HTfPr7qxC61w4TH/Zvk/KSJpOJhO2k5Y V2TtSfxKmo5TM5gDTzbrAODVo/8YHy81mJT5oO4qGVRfdaZJ0c7yabf1uuB1GTQdYENX tLfGjjKAWgOwwfBOBqgDFjZHC6vf5a2VwGzcsQPyD1QUdb1BmZBbpUxh2XEuzP5ZQbrT BKl2MvHp8PGppXGAS9j9N1vA3Ooq5XHelZErYWzQYb+Gk8ibF3K9C3MSljHqbLXuTn2b KQaKK2yH0hKpdb6jMIEcgX/pMEach07SNGELE9Ma0lUJf5kOQs641CXgXzFtfBNGnHE0 61lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=BmiQFSzRLq7R6UY1Tbv/pcPYh/URV7xmTZN8bD5iKlE=; b=rCHGGbgf2+9VQs0Uma5qP5G6aT3U2skuexBI0/RX9in/kmWgN23Yxzi4ahPZTLAIlD d6RsG9zWiWTMu0PJJCjQLMdN2CPK1j8IJivrYadbM/uY7Dfj2Py77iprZkFgDbBdCTIv l0C7uukFtuPya8vmmD/n1MHeAFjx/g6yUdiGWjwgEja53wryOdoAO07AlvlzgFS1CN3P EN5TVsuIzfFNvpQidK6LMIUqyr0Rzz3v/dumvVoT8UUziPuWui4x4CltgX5TIVUbAsdb 0lEcyTaa/GoFlxPintIGOQDv/gLOveMiESwrWxowQbRDuXUvykAG+JlZSfA7g5PI2ubP 7gbQ== X-Gm-Message-State: APjAAAWArn9OTNj5YbsVy1Q72PdJO1gtkhl6fIuLPmjb2seTFuHR8aDI X1itOL7p5MCzT32AEDR+cgTQOCtDyY9HVftgj4Wg7rk6 X-Google-Smtp-Source: APXvYqz8kxILur2FPhRt1Wi3IkWQZGsNtn/esjSvBWoMlgM111HxvYq1dmfUJ8ywZnAm/CNd2xngwOkO1maXPicpPN8= X-Received: by 2002:a5d:88d3:: with SMTP id i19mr26614763iol.187.1552555618174; Thu, 14 Mar 2019 02:26:58 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 14 Mar 2019 10:26:41 +0100 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000001d8cac05840a865c" Subject: Re: [RFC] Arrow functions / short closures From: nikita.ppv@gmail.com (Nikita Popov) --0000000000001d8cac05840a865c Content-Type: text/plain; charset="UTF-8" On Wed, Mar 13, 2019 at 4:56 PM Nikita Popov wrote: > Hi internals, > > Motivated by the recent list comprehensions RFC, I think it's time we took > another look at short closures: > > https://wiki.php.net/rfc/arrow_functions_v2 > > This is based on a previous (withdrawn) proposal by Levi & Bob. It uses > the syntax > > fn($x) => $x * $multiplier > > and implicit by-value variable binding. This example is roughly equivalent > to: > > function($x) use($multiplier) { return $x * $multiplier; } > > The RFC contains a detailed discussion of syntax choices and binding modes. > Trying to reply to everyone in one mail... 1. Using f() =>. This is possible. However, f would become a reserved keyword, and I'm very leery of making a single-character reserved keyword. Additionally I think that the fn() => syntax is more intuitive, but maybe that's my Rust influences speaking ;) 2. $this binding. Yes, $this is indeed bound exactly as with normal closures. I've explicitly mentioned this in the RFC now. 3. yield. Using yield is per-se not a problem. You can write `() => yield 42`, which is basically `function() { return yield 42; }`, which is okay, because yield is an expression and generators can return values since PHP 7. It also doesn't seem particularly useful though, as you're limited to yielding a single value. However, in the current form short closures are a replacement for list comprehensions only insofar they make the use of map() and filter() non-torturous. You couldn't actually use them to get a comprehension-like syntax as suggested in the other thread: $gen = (fn() => foreach ($list as $elem) yield $elem * 2))(); // would be, hypothetically: $gen = (function() use($list) { return foreach ($list as $elem) yield $elem * 2; }))(); The yield part here isn't the problem, "return foreach" is. Unlike yield, foreach is a statement, not an expression. To make this work we'd need a block form of short closures: $gen = (fn() { foreach ($list as $elem) yield $elem * 2; })(); // or maybe $gen = (fn() => { foreach ($list as $elem) yield $elem * 2; })(); Either way, I wouldn't want to read that code ;) The right way to write this is $list->map(fn($elem) => $elem * 2). 4. ($x) => $x instead of $x => $x. As Rowan mentioned, this doesn't really change things. Where $x is valid, ($x) is in most cases also valid. Regards, Nikita --0000000000001d8cac05840a865c--