Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104650 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15890 invoked from network); 11 Mar 2019 15:26:58 -0000 Received: from unknown (HELO mail-io1-f46.google.com) (209.85.166.46) by pb1.pair.com with SMTP; 11 Mar 2019 15:26:58 -0000 Received: by mail-io1-f46.google.com with SMTP id y6so3781025ioq.10 for ; Mon, 11 Mar 2019 05:16:36 -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 :cc; bh=NHJAFdcRBT7/VoMvoMyqfWKuCeXPfFbbG0wg7NUYfLI=; b=aU0CE2+/wlEiEuda5hyfoRDATc548dgfYK7TffziSchQwOVKt1xZWknLtameoGKG33 H0+QmciKe5pJ9hiZ6agJnOA80d69c71l6h9q/7OuWpeBPXjfkwJ8i2wltkZQDQ+V5XwL fytS0NLqFM4oN5RsLLduBCt++rTS3WaQyJ4n7SzpB5aIOVp+HjJsYyQac43t4aLDqAJ0 JrpxbYTLlfY1K/EiHzr+pV1KwZuc4m9FsGk9d3QQySMbQJJUxzsU4TUtkSUeA7inwdSw N079+xb/s7Bcks9Y0UO/87WYSb9AvNKUvViA1jWcfcc91TpvdJqe4KfNThaFOTrBdWSr z2IQ== 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:cc; bh=NHJAFdcRBT7/VoMvoMyqfWKuCeXPfFbbG0wg7NUYfLI=; b=iG12x3BSQnkGvPTxOXEkaC6dxZXvlAVDeIjUiai00IA6ko4mas7KTM2Fx32xBpFiVj d71ZgpYQPbndqlJMfwvuaXh0c+uBik82aXWH8b+Tka+U4SM/5//ZGh+Pqk4GotbMgGZl Uik9ueVsD9gMutoP9i1NvPGoUFsm+f1UlqHRpgknvgCuG2EQnsfwlor3hfURbUA9bD6w 7QHC8nEsmI7IXvctDWygPyuGG+wQ6jGZtAlKz39WLSc1B6jb2a8rVO30PhQ5ELyvSVWx 1eQFOSv5XbXPeSRhHnCCUYiabzJnT9zYEfqcv0jzNn6pVyO/pMrhMaa/33h/1bojKI71 ngZQ== X-Gm-Message-State: APjAAAWAK/lYJKA0E/+Mjyoq38yAiZZykitoHrY4UJQJgVKcaB+P/4Xm J/s/3bdNuKU/752BWgYV6ZYY9Iid9B/Zj/0qe38= X-Google-Smtp-Source: APXvYqw1J8TzR8hLQEshhkY4goOrOSzwE3SbqCUuhy1Gp0Q9qIxCABSQSqY2j7WUrdLBm5izSb/Bvxl3wa5bV6GT70Q= X-Received: by 2002:a5e:c906:: with SMTP id z6mr16103987iol.47.1552306595803; Mon, 11 Mar 2019 05:16:35 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 11 Mar 2019 13:16:19 +0100 Message-ID: To: Larry Garfield Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000039b71f0583d08b7f" Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: nikita.ppv@gmail.com (Nikita Popov) --00000000000039b71f0583d08b7f Content-Type: text/plain; charset="UTF-8" On Sun, Mar 10, 2019 at 10:45 PM Larry Garfield wrote: > Hello, peoples. I know it's been discussed once or twice before on the > list, many years ago, but not recently. I therefore feel OK putting forth > the following draft proposal for Comprehensions, aka "compact generators", > in PHP: > > https://wiki.php.net/rfc/cohttps://externals.io/message/61021mprehensions > > > Sara Golemon has written a preliminary patch that is partially complete > (see the RFC link for details, it's impressively simple), but unfortunately > doesn't have the bandwidth to complete it at this time. I am therefore > looking for collaborators with more knowledge of internals than I (which is > almost everyone) to help finish it up. > > The syntax proposed is also subject to revision if a terser but still > lexer-friendly alternative can be proposed. > > At the moment I'm not calling this Proposed yet, as I don't feel > comfortable doing so until someone else is on board to finish coding it. > That said, if someone wants to weigh in on the concept for now (hopefully > supportively) that's also fine. > > Anyone excited enough to help finish the job? > > (This is my first RFC collaboration, so if you're going to smack me for > goofing something please be gentle about it.) > I've brought up a similar proposal a few years ago, probably around the time generators were first introduced: https://externals.io/message/61021 I think my main point of feedback would be to stick closer to existing PHP syntax, even if it costs us some brevity. I would prefer $gen = [foreach ($list as $x) if ($x % 2) yield $x * 2]; over $gen = [for $list as $x if $x % 2 yield $x * 2]; The latter is nice in languages that generally use "for" for this purpose and generally allow omitting parentheses, but I don't think it's good to introduce this kind of syntax inconsistency in one place. Similarly, I found the ability to omit the "yield" expression somewhat confusing. Especially once you get to nesting $gen = [for $a as $b for $b as $c]; the behavior becomes non-obvious. Also wondering how this part interacts with keys, do you decide whether or not to yield the keys based on whether they are part of the for expression? $gen = [for $list as $v]; // drops keys $gen = [for $list as $k => $v]; // keeps keys Finally, Python makes a distinction between list comprehensions using [] and generator expressions using (). This proposal effectively corresponds to generator expressions, but uses the [] syntax. I'm wondering if that will cause confusion. Regards, Nikita --00000000000039b71f0583d08b7f--