Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 21, 2018 D's type declarations seem to read right to left. | ||||
---|---|---|---|---|
| ||||
D's type declarations seem to read right to left. int an_integer; int[10] an_array_of_10_integers; int[10]* a_pointer_to_an_array_of_10_integers = &an_array_of_10_integers; int*[10] an_array_of_10_pointers_to_integers; int*[10]* a_pointer_to_an_array_of_10_pointers_to_integers = &an_array_of_10_pointers_to_integers; int[3][2] an_array_of_2_arrays_of_3_integers; int[string] a_hashtable_with_string_keys_and_integer_values; int[3][string][2] an_array_of_2_hashtables_with_string_keys_and_array_of_3_integers_values; int function(float) a_pointer_to_a_function_that_takes_a_float_and_returns_an_integer; int function(float)[10] an_array_of_10_functions_that_take_floats_and_return_integers; I think this is a big improvement over C's "spiral" way of reading types: http://www.unixwiz.net/techtips/reading-cdecl.html I guess it could've been left to right, but... it's okay. |
March 21, 2018 Re: D's type declarations seem to read right to left. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tipdbmp | On Wednesday, 21 March 2018 at 20:07:09 UTC, tipdbmp wrote:
> I think this is a big improvement over C's "spiral" way of reading types:
Yes, D is perfect and has achieved sanity where before there was none.
You can read basically anything with little knowledge.
void function()[] array_of_funtions;
int[]*[]*[string] assoctiavive array of pointers to arrays of pointers to arrays of ints.
|
March 21, 2018 Re: D's type declarations seem to read right to left. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tipdbmp | On Wednesday, 21 March 2018 at 20:07:09 UTC, tipdbmp wrote:
> D's type declarations seem to read right to left.
>
>
> int an_integer;
>
> int[10] an_array_of_10_integers;
> int[10]* a_pointer_to_an_array_of_10_integers = &an_array_of_10_integers;
>
> int*[10] an_array_of_10_pointers_to_integers;
> int*[10]* a_pointer_to_an_array_of_10_pointers_to_integers = &an_array_of_10_pointers_to_integers;
>
> int[3][2] an_array_of_2_arrays_of_3_integers;
> int[string] a_hashtable_with_string_keys_and_integer_values;
>
> int[3][string][2] an_array_of_2_hashtables_with_string_keys_and_array_of_3_integers_values;
>
> int function(float) a_pointer_to_a_function_that_takes_a_float_and_returns_an_integer;
> int function(float)[10] an_array_of_10_functions_that_take_floats_and_return_integers;
>
>
> I think this is a big improvement over C's "spiral" way of reading types: http://www.unixwiz.net/techtips/reading-cdecl.html
> I guess it could've been left to right, but... it's okay.
The way I see it, English "of" flips its operands backwards compared to English's [adjective][noun] syntax:
int an_integer;
int* an_integer_pointer;
int[] an_integer_array;
int[3]* an_integer_array(length 3)_pointer;
But granted, in English, "of" is more scalable than [adjective][noun].
|
March 21, 2018 Re: D's type declarations seem to read right to left. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tipdbmp | On Wednesday, March 21, 2018 20:07:09 tipdbmp via Digitalmars-d-learn wrote:
> D's type declarations seem to read right to left.
>
>
> int an_integer;
>
> int[10] an_array_of_10_integers;
> int[10]* a_pointer_to_an_array_of_10_integers =
> &an_array_of_10_integers;
>
> int*[10] an_array_of_10_pointers_to_integers;
> int*[10]* a_pointer_to_an_array_of_10_pointers_to_integers =
> &an_array_of_10_pointers_to_integers;
>
> int[3][2] an_array_of_2_arrays_of_3_integers;
> int[string] a_hashtable_with_string_keys_and_integer_values;
>
> int[3][string][2] an_array_of_2_hashtables_with_string_keys_and_array_of_3_integers_values;
>
> int function(float)
> a_pointer_to_a_function_that_takes_a_float_and_returns_an_integer;
> int function(float)[10]
> an_array_of_10_functions_that_take_floats_and_return_integers;
>
>
> I think this is a big improvement over C's "spiral" way of reading types: http://www.unixwiz.net/techtips/reading-cdecl.html I guess it could've been left to right, but... it's okay.
In principle, D types read in more or less the same way as C types. The main differences are that we simplifed how function pointers are declared (making them more like function prototypes) and got rid of the types on the right-hand side of the name. e.g.
int[10] arr;
instead of
int arr[10];
Both read outward from the type, but the D way is then right-to-left in this case, whereas the C way is left-to-right even though they're both reading outward from the type. So, this makes it look like D reads right-to-left, but it'st still reading outward from the type. e.g. if you look at how dynamic arrays are declared, it's still outward from the type, not right-to-left. e.g.
auto foo = new int[][](10, 14);
assert(foo.length == 10);
assert(foo[0].length == 14);
vs
int[10][14] foo;
assert(foo.length == 14);
assert(foo[0].length == 10);
The fact that parens are used for const does muck with things a bit though. In C/C++, you have
const int* p;
which is equivalent to
int const* p;
but for whatever reason, both are allowed. The second one reads outward from the type, whereas the first one essentially breaks those rules a bit. And D essentially went with the first one, because the equivalent would be
const(int)* p;
and there's no way to put the const on the right. So, while the pointer portion of the type does read outward from the type, const doesn't really.
So, overall, D cleaned things up, but it didn't actually make them fully consistent either.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation