Re: RFC: Short syntax for Arrays (redux)
| From: | Justin Carmony | Date: | Wed, 01 Jun 2011 21:02:34 +0000 |
| Subject: | Re: RFC: Short syntax for Arrays (redux) | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-52704@lists.php.net to get a copy of this message | ||
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 shown
(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 = { 'name' => 'Justin',
'city' => 'ogden',
'state' => 'ut',
'country' => 'usa',
'favoriteNumbers' => [ 4, 12, 37, 42],
'unluckyNumbers' => [ 6, 13, 21],
'likesPhp' => 'very much so'
};
Characters: 192
Current way:
$person = new stdClass();
$person->city = 'ogden';
$person->state = 'ut';
$person->country = 'usa';
$person->favoriteNumbers = array(4, 12, 37, 42);
$person->unluckyNumbers = array(6, 13, 21);
$person->likesPhp = 'very much so';
Characters: 229
That is a 37 character difference. But the real driving factor is given PHP's lack of named
parameter syntax, passing objects and arrays (or sometimes 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 objects, 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 error so I can know a lot quicker what my
issue is.
As for syntax highlighters, IDEs, books, etc all being outdated, first off 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 highlighters, IDEs, and so forth when we introduced namespaces, and every IDE and
syntax highlighter I used updated very quickly to support them. I'm assuming 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 codes. 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 forcing anyone to use (1 == 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 <pierre.php@gmail.com> 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 = array('a' => 'b', 'c' => 'd');
>
> More than likely new:
> $foo = ['a' => 'b', 'c' => '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 => $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
>