Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85249 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 79607 invoked from network); 19 Mar 2015 23:23:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2015 23:23:58 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.181 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.181 mail-wi0-f181.google.com Received: from [209.85.212.181] ([209.85.212.181:34009] helo=mail-wi0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B1/D0-25408-D8A5B055 for ; Thu, 19 Mar 2015 18:23:57 -0500 Received: by wibg7 with SMTP id g7so17703844wib.1 for ; Thu, 19 Mar 2015 16:23:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=vx94ByPdzrQFLMezbwl/H1icVtLdBXUVOxlJtA3Iptw=; b=iVlNGZdg2xzL1vhTmskYPsV2rMQzBk1ijyQT01RcXLjStFkGOyS0WpRUlkpnUwzAHr WBQtXkckn10cUBtLiDYoENOskeKiJlcf/RcGp7fvRXnbMZ/dr0p/ulm9RqMo9/6CEQqO a2zqi8K/83vjLdLj40IiA6x+K+jl86/A8knQVKq1z81dyOH0FiMQ+xpat3JQjW/d1s0C PWAcXSCpk3G35VYNGZbPhI8io37SauyfvsjBUJvd9rARiLxx3EPF3pAfJASD4sdMx3sD vvumdsswmvvdizQ8H3uqi2MSYVRzjZpIs0kTAjAWVrhnxEN082g0WoOpQbXCme7Rkxhn aHow== X-Received: by 10.180.8.165 with SMTP id s5mr770278wia.16.1426807434545; Thu, 19 Mar 2015 16:23:54 -0700 (PDT) Received: from [192.168.0.5] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id e18sm3905739wjz.27.2015.03.19.16.23.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Mar 2015 16:23:53 -0700 (PDT) Message-ID: <550B5A81.1090706@gmail.com> Date: Thu, 19 Mar 2015 23:23:45 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RFC - Array slice syntactic sugar From: rowan.collins@gmail.com (Rowan Collins) On 19/03/2015 20:49, Alex Bowers wrote: > My proposal is something similar to Pythons slice, in PHP this would look > like: > > $slided = $array[1:4] > > This will get the elements in positions 1,2,3,4. (1 through 4 inclusive), > ignoring the actual key of the array. The result for an array will be an > array with the keys preserved, in the same order. While I can see the reasoning for not looking into the actual keys (which might not have a logically sliceable order), I think the syntax needs to be more distinct, because this seems a bit confusing: $countdown = [ 5 => 'Five!', 4 => 'Four!', 3 => 'Three!', 2 => 'Two!', 1 => 'One!', 0 => 'Zero!' ] $countdown[0]; // 'Zero!' $countdown[0:0]; // ['Five!'] $countdown[0:][0] // 'Zero!' $countdown[:0][0] // null - slice contains no key 0 The problem is that the slice access looks like an extension of key access, but is actually a new concept of positional element access. With a slight tweak to syntax, such as adding an @, we could make positional access available for non-slices as well: reset($countdown); // Five! - a common way of getting the first element positionally $countdown[@0]; // 'Five!' $countdown[@1]; // 'Four!' $countdown[@0:0] // ['Five!'] $countdown[@0:1] // ['Five!', 'Four!'] $countdown[@0:][@0] // 'Five!' $countdown[@:0][@0] // 'Five!' > A side addition, that may be considered is adding [-1] to get the last item > in the array. But that is not a main part of this RFC. This would actually be a really important addition for me, but only if I can do it on a non-slice, as above: $countdown[-1]; // null - no such key end($countdown); // 'Zero!' - common way of getting last element $countdown[@-1]; // 'Zero!' - positional access counting from the end $countdown[@-2]; // 'One!' - fiddly to do in current PHP I just wrote out all those examples and realised that it may not be possible to reuse @ in this context. Finding new symbols to use for syntax is hard. :( Regards, -- Rowan Collins [IMSoP]