Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101710 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95256 invoked from network); 26 Jan 2018 21:51:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jan 2018 21:51:47 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.85 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.85 mx104.easyname.com Received: from [77.244.243.85] ([77.244.243.85:46155] helo=mx104.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F5/0A-35287-EE2AB6A5 for ; Fri, 26 Jan 2018 16:51:43 -0500 Received: from cable-81-173-135-182.netcologne.de ([81.173.135.182] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1efBuF-0002Nd-2G; Fri, 26 Jan 2018 21:51:32 +0000 Reply-To: internals@lists.php.net To: Christian Schneider , PHP Internals References: <517F0F7B-CD01-4F35-9021-8336B7BA06E1@cschneid.com> Message-ID: <620452cc-09eb-ec95-bf8d-e1cddee988f1@fleshgrinder.com> Date: Fri, 26 Jan 2018 22:51:33 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <517F0F7B-CD01-4F35-9021-8336B7BA06E1@cschneid.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-DNSBL-PBLSPAMHAUS: YES X-DNSBL-SURBL: YES Subject: Re: [PHP-DEV] Shorthand proposal for associative arrays From: php@fleshgrinder.com (Fleshgrinder) On 1/26/2018 7:16 PM, Christian Schneider wrote: > Hi there, > I have a proposal for a shorthand notation of associative arrays borrowed from another language: > :$foo > would be equivalent to > 'foo' => $foo > and would work with array, list or [] > > Motivation behind it, maybe someone else finds more good uses: > > 1) Emulating named parameters with associative arrays like > html::img([ 'src' => $src, 'alt' => $alt ]); > could be written as > html::img([ :$src, :$alt ]); > which encourages consistent naming of variables and parameters > > 2) Simplifying list destructuring with non-integer keys, example taking from http://php.net/manual/en/migration71.new-features.php#migration71.new-features.support-for-keys-in-list > foreach ($data as ["id" => $id, "name" => $name]) { > becomes > foreach ($data as [ :$id, :$name ]) { > which reduces redundancy. > > I implemented a minimal patch (2 lines are added to the parser) to implement this which you can find at > https://cschneid.com/php/php7_2/assoc_array_shorthand.patch > > What do you think, is this worth an RFC? I hope I didn't miss an existing one :-) > > Regards, > - Chris > > Hi Chris! I really like this proposal. `compact` is cumbersome to use, or lets say, almost impossible without an intelligent IDE. However, what is more important is the fact that is would allow for a very readable and usable approach for destructuring of associative arrays (as illustrated by your example) which is very, very neat. The change is also extremely minimal which speaks for it. I would support you in writing up an RFC for this (if desired). -- Richard "Fleshgrinder" Fussenegger