Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:52711 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 29684 invoked from network); 1 Jun 2011 22:09:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jun 2011 22:09:47 -0000 Authentication-Results: pb1.pair.com smtp.mail=johncrenshaw@priacta.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=johncrenshaw@priacta.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain priacta.com designates 64.95.72.238 as permitted sender) X-PHP-List-Original-Sender: johncrenshaw@priacta.com X-Host-Fingerprint: 64.95.72.238 mx1.myoutlookonline.com Received: from [64.95.72.238] ([64.95.72.238:5083] helo=mx1.myoutlookonline.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 39/54-32367-AA8B6ED4 for ; Wed, 01 Jun 2011 18:09:46 -0400 Received: from st23.mx1.myoutlookonline.com (localhost [127.0.0.1]) by mx1.myoutlookonline.com (Postfix) with ESMTP id 9FBBE416D44 for ; Wed, 1 Jun 2011 18:09:43 -0400 (EDT) X-Virus-Scanned: by SpamTitan at mail.lan Received: from HUB023.mail.lan (unknown [10.110.2.1]) by mx1.myoutlookonline.com (Postfix) with ESMTP id 00A48416D3E for ; Wed, 1 Jun 2011 18:09:43 -0400 (EDT) Received: from MAILR001.mail.lan ([192.168.1.2]) by HUB023.mail.lan ([10.110.17.23]) with mapi; Wed, 1 Jun 2011 18:08:38 -0400 To: PHP internals Date: Wed, 1 Jun 2011 18:09:36 -0400 Thread-Topic: [PHP-DEV] RFC: Short syntax for Arrays (redux) Thread-Index: AcwgoU8xIPH6ujh0R8W5pGlfEDG9igABjvVw Message-ID: References: <4DE5368A.6050603@moonspot.net> <8BEEEE49-8DA3-4634-BF9C-120F7A15B613@roshambo.org> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: RE: [PHP-DEV] RFC: Short syntax for Arrays (redux) From: johncrenshaw@priacta.com (John Crenshaw) For small linear arrays, neither format is "more readable" but for even mil= dly complex structures, there is a clear difference. Consider the following= Mongo query: $query =3D array( 'time'=3D>array('$and'=3D>array( array('$gt'=3D>strtotime('-1 day')), array('$lt'=3D>time()), )), '$or'=3D>array( array('foo'=3D>'bar'), array('hello'=3D>'world') ) ); Vs. the JSON form: $query =3D { time: {'$and': [ {'$gt': strtotime('-1 day')}, {'$lt': time()}, ]}, '$or': [ {foo: 'bar'}, {hello: 'world'} ] }; Because of the clear readability difference I'll take anything, but JSON wo= uld be much better than just an "array shorthand". John Crenshaw Priacta, Inc. -----Original Message----- From: Anthony Ferrara [mailto:ircmaxell@gmail.com]=20 Sent: Wednesday, June 01, 2011 5:18 PM To: PHP internals Subject: Re: [PHP-DEV] RFC: Short syntax for Arrays (redux) Personally, I think focusing on a character savings is the wrong reason to take on any problem. I don't care how long it takes for me to type code. I don't care how much extra hdd space it takes. But what I do care about is how readable it is. To me, the array shortcut syntax is pointless. Do you really mean to tell me that `[1, 2]` is more readable/easier to understand than `array(1,2)`? To me, it's about a wash. However, the object shortcut syntax is a major win. For example: $person =3D new stdClass(); $person->city =3D 'ogden'; $person->state =3D 'ut'; $perspn->country =3D 'usa'; $person->favoriteNumbers =3D array(4, 12, 37, 42); $person->somethingWithNumbers(); $person->unluckyNumbers =3D array(6, 13, 21); $person->likesPhp =3D 'very much so'; vs $person =3D { 'name' =3D> 'Justin', 'city' =3D> 'ogden', 'state' =3D> 'ut', 'country' =3D> 'usa', 'favoriteNumbers' =3D> [ 4, 12, 37, 42], 'unluckyNumbers' =3D> [ 6, 13, 21], 'likesPhp' =3D> 'very much so' } Did you notice what happened there? There's two differences. Try to find them. Neither would be possible with the shortcut syntax. Now, the only reason I would personally support the array shortcut is if it was an implementation of JSON. I know that's not on the table here, but that's the only benefit I can see to implementing a shortcut for arrays (that would save 5 characters per instance). Just my $0.02... Anthony On Wed, Jun 1, 2011 at 5:02 PM, Justin Carmony w= rote: > To address the soapbox: > > Its not just to reduce the five characters at the beginning, but when you= have more complex structures as well. There was already a great example sh= own (http://paste.roguecoders.com/p/0747f2363c228a09e0ddd6f8ec52f2e8.html) = of that. Also, if object support is added (which we need to add to the RFC)= , you can cut down on a lot more verbose code, especially with objects. > > $person =3D { 'name' =3D> 'Justin', > =A0 =A0'city' =3D> 'ogden', > =A0 =A0'state' =3D> 'ut', > =A0 =A0'country' =3D> 'usa', > =A0 =A0'favoriteNumbers' =3D> [ 4, 12, 37, 42], > =A0 =A0'unluckyNumbers' =3D> [ 6, 13, 21], > =A0 =A0'likesPhp' =3D> 'very much so' > }; > > Characters: 192 > > Current way: > > $person =3D new stdClass(); > $person->city =3D 'ogden'; > $person->state =3D 'ut'; > $person->country =3D 'usa'; > $person->favoriteNumbers =3D array(4, 12, 37, 42); > $person->unluckyNumbers =3D array(6, 13, 21); > $person->likesPhp =3D 'very much so'; > > Characters: 229 > > That is a 37 character difference. But the real driving factor is given P= HP's lack of named parameter syntax, passing objects and arrays (or sometim= es a mix, depending on the framework) is becoming very popular. So not only= do you save some typing just once, but if you use this pattern a lot, you = save a lot of typing over your entire project. Also, when dealing with obje= cts, I have to make sure I retype "person" correctly each time. If I don't,= I'll get a notice error. But with the new syntax, it'll throw a parsing er= ror so I can know a lot quicker what my issue is. > > As for syntax highlighters, IDEs, books, etc all being outdated, first of= f no one is suggesting to deprecate the array() function. So you will only = use this new syntax if you choose to do so. Second, we broke syntax highlig= hters, IDEs, and so forth when we introduced namespaces, and every IDE and = syntax highlighter I used updated very quickly to support them. I'm assumin= g the IDEs spent a great deal more time adding Namespacing support than it = will to support a short syntax for arrays and objects. > > PHP has made short syntax for other things, such a if statement short cod= es. Yet many books don't cover it, since it is one of those things you read= in the documentation later and decide "Do I want to use this?" No one is f= orcing anyone to use (1 =3D=3D 1 ? true : false) type of if/then logic. The= same will work with the new syntax. > > My two cents. > > Justin > > On Jun 1, 2011, at 2:37 PM, Michael Shadle wrote: > >> On Wed, Jun 1, 2011 at 1:01 PM, Pierre Joye wrote= : >> >>> I modified the vote page, pls move your votes to the desired syntax >>> (or global -1) >> >> This is a good idea to group things like this. >> >> Back on the soapbox. All of this is just to reduce typing "array" (5 >> characters) before things? >> >> Old: >> $foo =3D array('a' =3D> 'b', 'c' =3D> 'd'); >> >> More than likely new: >> $foo =3D ['a' =3D> 'b', 'c' =3D> 'd']; >> >> 5 character difference for each array being saved. That's it. At the >> expense of syntax highlighters, IDEs, books, all becoming outdated and >> need to be updated. For a language construct that has been around for >> what, 10 years? >> >> Oh, and for anyone desiring a ":" for this new shorthand, why stop at >> array shorthand. Why not change this from: >> foreach($foo as $key =3D> $val) >> >> To: >> foreach($foo as $key: $val) >> >> That would save one character for each array iteration like that. >> >> Also - if we're worried about saving characters and shorthand why not >> just remove the "$" language construct? That's a LOT of keystrokes. In >> my WordPress install, that's 75,412 characters saved. Versus 6,960 >> "array(" matches, which would save 34,800 characters. >> >> These were quick examples from a coworker. Just another PHP user who >> said "wait why would they make another way to express an array?" >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > --=20 PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php