Hello, folks! Newbie to D, have some questions!

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 20 07:27:16 PST 2017


On 02/20/2017 03:44 PM, timmyjose wrote:
> Things I don't like so much:
>
> 1). The std.range: iota function(?) is pretty nice, but the naming seems
> a bit bizarre, but quite nice to use.

Yeah, the name is weird. A little googling suggests it comes from C++ 
[1] which took it from APL.

> 2). The automatic conversion rules are nice for avoiding verbose code,
> but it looks like it might bite one just like in C++.

D at least disallows narrowing conversions. But yeah, conversions 
between signed/unsigned, from integral to floating point, or from 
narrower to wider char variants can have surprising results.

> 3). Not so much a fan of "auto", but it does have its uses, of course.

`auto` can obscure your code, but it can also make it more DRY. And with 
ranges and their combinations, types quickly get too complex to type out.

> 4). I'm still a bit confused by order of dimensions in rectangular arrays:
>
> Suppose I have a simple 2 x 3 array like so:
>
> import std.stdio;
> import std.range: iota;
>
> void main() {
>     // a 2 x 3 array
>     int [3][2] arr;
>
>     foreach (i; iota(0, 2)) {
>         foreach(j; iota(0, 3)) {
>             arr[i][j] = i+j;
>         }
>     }
>
>     writefln("second element in first row = %s", arr[0][1]);
>     writefln("third element in second row = %s", arr[1][2]);
>
>     writeln(arr);
> }
>
> My confusion is this - the declaration of the array is arr
> [last-dimension]...[first-dimension], but the usage is
> arr[first-dimension]...[last-dimension]. Am I missing something here?

You've got it. Declarations have the form `Type name;`. Fixed-size array 
types have the form `E[n]`. E can itself be another fixed-size array 
type, say F[m]. Then the whole type becomes F[m][n]. Simple.

The syntax could have be designed to grow in the other direction: [n]E = 
[n][m]F, to match indexing order. But Walter didn't make it that way.


[1] http://en.cppreference.com/w/cpp/algorithm/iota


More information about the Digitalmars-d-learn mailing list