RE: Proposal: Array syntax

php.internals

Ford, Mike [LSS]

22 years ago
On 05 November 2003 17:06, Marco Tabini contributed these pearls of wisdom:
> Christian Schneider wrote: >> Marco Tabini wrote: >> >>> $a = [[1,2,3],[1=>[1,3,2,2], "a"=>[[1,2,3,4],4,[1,2]]]; >> >> >> $a = array(array(1,2,3),array(1=>array(1,3,2,2), >> "a"=>array(array(1,2,3,4),4,array(1,2))); >> > > Besides my previous points, something even more abominable: > > $a = [1,2,$b[11]]; > > Is that confusing enough for you? ;-)
What's confusing about it? Cheers! Mike
-- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@leedsmet.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

Marco Tabini

22 years ago
Ford, Mike [LSS] wrote:
> On 05 November 2003 17:06, Marco Tabini contributed these pearls of wisdom: > > >>Christian Schneider wrote: >> >>>Marco Tabini wrote: >>> >>> >>>>$a = [[1,2,3],[1=>[1,3,2,2], "a"=>[[1,2,3,4],4,[1,2]]]; >>> >>> >>>$a = array(array(1,2,3),array(1=>array(1,3,2,2), >>>"a"=>array(array(1,2,3,4),4,array(1,2))); >>> >> >>Besides my previous points, something even more abominable: >> >>$a = [1,2,$b[11]]; >> >>Is that confusing enough for you? ;-) > > > What's confusing about it? >
The fact that $b[11] references an item of an array, while [1,2,$b[11]] assigns values to the array $a. The fact that you (and, probably, most of us) can't tell right off the bat is a clear sign that this is a bad idea, because it's ambiguous and confusing. The same line using the current syntax, btw, would have looked like this: $a = array (1,3,$b[11]); As you can see the ambiguity is gone--square brackets are used for one purpose and nothing else. Cheers, Marco

Michael Walter

22 years ago
>>> $a = [1,2,$b[11]]; >>> >>> Is that confusing enough for you? ;-) >> >> >> >> What's confusing about it? >> > > The fact that $b[11] references an item of an array, while [1,2,$b[11]] > assigns values to the array $a. The fact that you (and, probably, most > of us) can't tell right off the bat is a clear sign that this is a bad > idea, because it's ambiguous and confusing. > > The same line using the current syntax, btw, would have looked like this: > > $a = array (1,3,$b[11]);
Actually, $a = [1,2,$b[11]] would be amazingly clear and expressive in comparison with the rather verbose array() version (same thing with the swap, btw). BTW, remember the alternative range syntax [a..b] I mentioned before? If you consider Markus Boegers (sp?) iterators extension, wouldn't it be top cool to have [a..] syntax, too (yielding an iterator)? And have versions of map/filter/reduce support iterators, too? :) Well, just thinking out loud, you remember ;)
> As you can see the ambiguity is gone--square brackets are used for one > purpose and nothing else.
Actually, do you realize that you use () both for "grouping" and for application? I can't see anything wrong with using square brackets for array element access and array creation, to be honest. Cheers, Michael

Marco Tabini

22 years ago
Michael Walter wrote:
>>>> $a = [1,2,$b[11]]; >>>> >>>> Is that confusing enough for you? ;-) >>> >>> >>> >>> >>> What's confusing about it? >>> >> >> The fact that $b[11] references an item of an array, while >> [1,2,$b[11]] assigns values to the array $a. The fact that you (and, >> probably, most of us) can't tell right off the bat is a clear sign >> that this is a bad idea, because it's ambiguous and confusing. >> >> The same line using the current syntax, btw, would have looked like this: >> >> $a = array (1,3,$b[11]); > > Actually, $a = [1,2,$b[11]] would be amazingly clear and expressive in > comparison with the rather verbose array() version (same thing with the > swap, btw).
I guess we'll have to agree to disagree :) $a = [1,2,$b[11]] is semantically inconsistent.
>> As you can see the ambiguity is gone--square brackets are used for one >> purpose and nothing else. > > Actually, do you realize that you use () both for "grouping" and for > application? I can't see anything wrong with using square brackets for > array element access and array creation, to be honest.
Actually, no, I don't. I'm not sure what "grouping" and "application" mean... Mt.

Michael Walter

22 years ago
> I guess we'll have to agree to disagree > $a = [1,2,$b[11]] is semantically inconsistent.
Yeah, I agree to disagree on that one, too :)
>> Actually, do you realize that you use () both for "grouping" and for >> application? I can't see anything wrong with using square brackets for >> array element access and array creation, to be honest. > > > Actually, no, I don't. I'm not sure what "grouping" and "application" > mean...
$a*(1+2) vs. $a(1+2) "Grouping" (not really a good term, I'm sorry) in the sense of determining the order of evaluation. "Application" being normal function application (function call). Cheers, Michael

George Schlossnagle

22 years ago
On Nov 5, 2003, at 1:29 PM, Marco Tabini wrote:
> $a = [1,2,$b[11]] is semantically inconsistent.
How so? Is foo(array(1,2)); semantically inconsistent? On one hand () is used with a language construct (array()), whereas in the other context it indicates arguments to a function. I think that is what he meant by 'grouping' and 'application'. Similar tokens have different syntactical meaning all over the language. Compare '<<' and '<<<'. To me that is no diffent that <variable>[] and []. George (still -1, but because it makes for unmaintainable code, not because it's hard to read)

Marco Tabini

22 years ago
George Schlossnagle wrote:
> > On Nov 5, 2003, at 1:29 PM, Marco Tabini wrote: > >> $a = [1,2,$b[11]] is semantically inconsistent. > > > How so? Is >
I think I've already explained why.
> foo(array(1,2)); > > semantically inconsistent? On one hand () is used with a language > construct (array()), whereas in the other context it indicates arguments > to a function. I think that is what he meant by 'grouping' and > 'application'. >
Actually, I think it meant something else. In any case, semantically you can still think of array() as a function (until you throw in key declarations, of course ;) )
> Similar tokens have different syntactical meaning all over the > language. Compare '<<' and '<<<'. To me that is no diffent that > <variable>[] and [].
Again, that's not a good reason to introduce more. Mt.

Michael Walter

22 years ago
Marco Tabini wrote:
> George Schlossnagle wrote: > >> >> On Nov 5, 2003, at 1:29 PM, Marco Tabini wrote: >> >>> $a = [1,2,$b[11]] is semantically inconsistent. >> >> >> >> How so? Is >> > > I think I've already explained why.
Not really understandable, though.
> >> foo(array(1,2)); >> >> semantically inconsistent? On one hand () is used with a language >> construct (array()), whereas in the other context it indicates >> arguments to a function. I think that is what he meant by 'grouping' >> and 'application'. >> > > Actually, I think it meant something else. In any case, semantically you > can still think of array() as a function (until you throw in key > declarations, of course ;) )
Yup, I told you I meant something different (although on the same line of reasoning). I think you misunderstand "semantics", anyway. You should not think of array() as a function, as basically everything else except to the syntax is different to "real" functions, consider call_user_func('array', 1, 2, 3) or array_map('array', array(1, 2, 3), array("Foo", "Bar", "Baz")); How much better would look map('array', [1..3], ["Foo", "Bar", "Baz"]); anyway.. (although we really should not argue about that, as it's simply taste).
>> Similar tokens have different syntactical meaning all over the >> language. Compare '<<' and '<<<'. To me that is no diffent that >> <variable>[] and []. > > > Again, that's not a good reason to introduce more.
That is a statement backed up by good argumentation. :)