Is this a bug?

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 28 05:18:39 PDT 2014


Andrey:

> alias short Type1;

The "alias X Y;" syntax is going to be deprecated, so use "alias 
Y = X;" if your compiler already supports it.


> alias Type1[100]* Type2; // if I take out '*' I will have to 
> type it everywhere, because arrays in D2 always 'by value'

Adding the * everywhere could be a good thing, because it makes 
the code more explicit. Even in Linux kernel typedefs that just 
mask out a pointer are not appreciated a lot. D code that uses 
pointers to fixed size arrays is very uncommon in D, so adding a 
* there is not going to cause troubles.


> struct Type3
> {
> 	Type1 key;
> 	int  flags;
>         int  arrLen;
> 	Type2 f; // this struct can be *optionally* extended by the 
> array
> }

The common way to create a variable length struct in D is to use 
a zero length fixed size array (and no pointers):

struct Type3 {
     Type1 key;
     int flags;
     int arrLen;
     Type1[0] f;
}


But if you don't mind the double indirection using a dynamic 
array is simpler:

struct Type3 {
     Type1 key;
     int flags;
     Type1[] f;
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list