April 03, 2013 Re: Differing semantics between multidimensional fixed-length array and slice initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Cain | On Wednesday, 3 April 2013 at 02:28:55 UTC, Chris Cain wrote:
> On Wednesday, 3 April 2013 at 01:30:19 UTC, ixid wrote:
>> How can you call the new syntax better? You assign arrays' lengths in the opposite to that that you access them. It's a horrible design mistake.
>
> Unfortunately, I have to agree that the new syntax is better. int[5][6] is confusing as a type because it's read from right to left, but it makes much more sense than int[6][5].
>
> Consider how you would read the "old style"
>
> It's a 6-long array of 5-long arrays of int.
>
> So, the new style is simply read from right to left. But using the other way ... it's read from the center to right, and finally to the front.
>
> How about throwing AAs into the mix?
>
> int[string][5][6]
>
> What would that mean? The old style would be incredibly confusing. It'd be an associative array mapping strings to 5-long arrays of 6-long arrays of ints. Right?
>
> Plus consider this:
>
> alias Dict = int[string]
>
> Dict[5][6] vs int[string][5][6]
>
> Using the new way, these two are the same. Using the "old style" ... they're not.
>
>
> Really though, bearophile has it right. C (and, D, transitively) made a huge mistake in type declarations. They ought to be read from left to right (like Pascal). C is crazy in that they've designed it so you read types in a spiral pattern.
>
> What's a int*? It's a pointer to an int. Why isn't it a *int, then? How about [5]int? It's a 5-long array of int. [5][6]int ... It's a 5-long array of 6-long arrays of integers. And so on.
I wasn't arguing for the old style, the new declarations should have read from left to right so [6][5][string]int and then you access the elements in the same order that you assigned them, that seems far less confusing and leaves less room for sleepy mistakes. It seems like a pointless mental overhead so have to keep swapping the order of your arguments.
|
April 03, 2013 Re: Differing semantics between multidimensional fixed-length array and slice initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Cain | On 04/02/2013 07:28 PM, Chris Cain wrote:> On Wednesday, 3 April 2013 at 01:30:19 UTC, ixid wrote: > What's a int*? It's a pointer to an int. Why isn't it a *int, then? I wonder whether it would complicate syntax? (But * already has so many meanings that maybe that would not be too bad.) > How about [5]int? It's a 5-long array of int. [5][6]int ... It's a 5-long > array of 6-long arrays of integers. And so on. [5] is already an array literal of one element. I would have to be an exception to the current syntax. Still confusing... Ali |
April 03, 2013 Re: Differing semantics between multidimensional fixed-length array and slice initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | On Wednesday, 3 April 2013 at 15:44:52 UTC, ixid wrote:
>
> I wasn't arguing for the old style, the new declarations should have read from left to right so [6][5][string]int and then you access the elements in the same order that you assigned them, that seems far less confusing and leaves less room for sleepy mistakes. It seems like a pointless mental overhead so have to keep swapping the order of your arguments.
Sorry for the misunderstanding, then. But to be fair, I'm sure that monarch_dodra meant that the new style is better than the old style, so my inference that you disagreeing with him saying the new style is better than the old style meant that you thought the old style was better isn't entirely unfounded.
I do have to agree that an even better syntax would have been to read types from left to right. However, that ship has sailed long ago (plus, I think the decision was made to make it more C-like in nature, which has its benefits as well).
|
April 03, 2013 Re: Differing semantics between multidimensional fixed-length array and slice initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 3 April 2013 at 15:56:31 UTC, Ali Çehreli wrote: > I wonder whether it would complicate syntax? (But * already has so many meanings that maybe that would not be too bad.) I doubt it, but maybe. Plenty of languages exist that do a left-to-right reading of types and they seem to work out okay. Plus, reversing the order doesn't seem like it'd make a difference. > > > How about [5]int? It's a 5-long array of int. [5][6]int ... > It's a 5-long > > array of 6-long arrays of integers. And so on. > > [5] is already an array literal of one element. I would have to be an exception to the current syntax. Still confusing... It's no different than the current syntax from that regard. An type named int followed by an array literal of one element is no less confusing than an array literal of one element followed by a type named int. It has an overloaded meaning in both cases. That said, many other languages say things like "array (0..5) of Integer" and such, which removes that overloaded meaning, but it has the problem of being a bit too verbose (IMO). It does make a difference if you're reading/writing many thousands of lines of code. Heck, even the current syntax is verbose enough to warrant type inference in many cases. |
Copyright © 1999-2021 by the D Language Foundation