Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104778 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58216 invoked from network); 17 Mar 2019 22:55:47 -0000 Received: from unknown (HELO localhost.localdomain) (76.75.200.58) by pb1.pair.com with SMTP; 17 Mar 2019 22:55:47 -0000 To: internals@lists.php.net References: <85417a73-a391-c9ad-49aa-5b88c65b0900@gmail.com> Date: Sun, 17 Mar 2019 20:46:58 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.2 MIME-Version: 1.0 In-Reply-To: <85417a73-a391-c9ad-49aa-5b88c65b0900@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 46.195.64.167 Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: ajf@ajf.me (Andrea Faulds) Message-ID: Hi, Stanislav Malyshev wrote: > >> 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. > > Do we need this distinction for anything useful? > In Python, the difference is that []-syntax gives you a list (pre-comupted), whereas without the [] you get a generator (generate-on-demand). This distinction is important because the generator might be iterating over something non-repeatable (e.g. another generator), or have some destructive or mutating effect. You also might not want to take the performance penalty of computing it every time you iterate over it. Why not apply the same approach to PHP? There is iterator_to_array() to convert a generator to an array, so we may not need both syntaxes. However, I think using [] for something that is *not an array* is counter-intuitive. Thanks, Andrea