Re: Syntactic improvement to array
| From: | Gregory Beaver | Date: | Mon, 05 Feb 2007 05:13:31 +0000 |
| Subject: | Re: Syntactic improvement to array | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-27788@lists.php.net to get a copy of this message | ||
Andi Gutmans wrote:
> Hi,
>
> I thought I may have brought this up a long time ago but couldn't find anything in the
> archives.
> For a long time already I've been thinking about possibly adding a new syntax for
> array(...) which would be shorter. I'd suggest
> [...]. While I am usually not in favor of having more than one way to do things, I think
> it'd look much more elegant especially (but
> not only) for nested arrays.
>
> So what I'm thinking of is:
> array(1, 2, 3) == [1, 2, 3]
> array(1, 2, array("foo", "bar")) == [1, 2, ["foo",
> "bar"]]
> array("key" => 1, "key2" => 2) == ["key" => 1,
> "key2" => 2]
>
> $arr = [1, 2, 3]
> vs.
> $arr = array(1, 2, 3)
>
> Well enough examples given :)
> I think it's not worth doing unless there's overwhelming support as it's not
> desperately needed. But I'd be interested to hear
> people's thoughts. It seems implementation shouldn't be an issue but I'd have to
> dive a bit deeper.
future:
<?php
$a = [1, 2, 3];
$a[1] = [1];
$a = [1];
[$a] = $a;
$a[] = [];
echo $a.[1]; // not a parse error
?>
now:
<?php
$a = array(1, 2, 3);
$a[1] = array(1);
$a = array(1);
list($a) = $a;
$a[] = array();
echo $a.[1]; // now a parse error
?>
For what it's worth, the examples above show why I'm a strong -1 on this
idea. It introduces the possibility of weird misunderstandings that
just don't exist now, and also introduces a new way to accidentally
break your script that wasn't possible before.
The only way I could see solving this would be:
1) [] != list()
2) use a[] as in $a = a[1, 2, 3] but then we might as well stick with
array().
Either way, [] is overloaded in the new definition:
1) array access
2) ArrayAccess object offsetGet/offsetSet
3) incremental index ($a[] = 1;)
4) array creation
[5) list()]
Whereas now, it is only 3 possible meanings, all of which are consistent
with a few quirks in the ArrayAccess one as of PHP 5.2.
Greg