What's left for 1.0?

Bill Baxter dnewsgroup at billbaxter.com
Sat Nov 18 07:10:10 PST 2006


Walter Bright wrote:
> Bill Baxter wrote:
>> Walter Bright wrote:
>>  > C can't do any of those things.
>>
>> Sure it can.
>>
>>>> * No way to initialize a static array without counting elements
>>>>     static byte[???] imageData = [...];   // i hope you like counting!
>>
>> C has no problem with this.  I do it all the time:
>>
>>        static const unsigned char[] = { 1,2,3,4,5,6,7, 255 };
> 
> True, but that's only because C doesn't have dynamic arrays. In D,
> 
> const char[] c = [ 1,2,3,4,5,6,7, 255 ];
> 
> works fine, though it's a dynamic array.

Yep it's great that D has built-in dynamic arrays, but the point is that 
the syntax for dynamic arrays is getting in the way of static arrays, 
making something that's simple in C become hard in D.  If you want a 
static array you have no choice right now but to count up the elements, 
or deliberately use the wrong length to trigger compiler errors that 
will tell you the right length.

If D has some other convenient and portable way to embed data like icons 
and images into one's exe, then I'd be interested in hearing about it, 
but for right now it seems to me like working with static data is a pain 
in D.

Anyway, this one thing has actually been the biggest annoyance I've run 
across in trying to port C++ code to D.  Most things become easier, but 
this one thing is harder in D.

>>>> * No way to initialize a dynamic array with a known length
>>>>     byte[] imageData;  imageData.length = 5;  // two steps - meh
>>
>> C has no problem with this (using malloc, its own concept of "dynamic 
>> arrays"):
>>
>>    byte* imageData = (byte*)malloc(5*sizeof(byte));
> 
> D:    auto imageData = new byte[5];

Ok, that's good enough for that one.  Thanks.  I didn't realize new'ing 
an array like that was allowed (and compatible with setting .length).

>>>> * No way to initialize array of strings
>>>>     char[][] list = ["eggs","bacon","milk","break"];  //uh uh
>>
>> C can do this:
>>
>>     char *list[] = { "eggs","bacon","milk","break" };
> 
> So can D:
> char *list[] = [ "eggs","bacon","milk","break" ];
 > char[] list[] = [ "eggs","bacon","milk","break" ];

Nope, D cannot:
    dchar.d(12): Error: cannot implicitly convert expression ("bacon") 
of type char[5] to char[4]

As someone pointed out though, you can make it work by making everything 
dynamic:
     char[] list[] = [ "eggs"[],"bacon","milk","break" ];

But, then you're making everything dynamic when it should be static. 
Correct me if I'm wrong, but in the C/C++ version of this above, 
basically everything is static.  The strings will be embedded into the 
exe, and the array will just consist of pointers directly to those 
strings in the data segment.  But in the D version you'll have that same 
data in the data segment, and then you'll also make dynamic copies of 
all the data onto the heap at runtime.  I don't want two copies of all 
my static data, especially if one of those copies requires runtime heap 
allocations.

>>>> * No way to initialize non-static struct
>>>>     Point p = { x:1.0, y:2.0 };  // nope...not static
>>
>> C has no problem with that either:
>>
>>     struct Point { float x, y; };
>>     void foo() {
>>        Point p = {1.0,2.0};
>>     }
> 
> True. I forgot it could (replacing "Point" with "struct Point").

Right -- struct Point.  It's been ages since I had to deal with C's 
"typedef struct foo { } foo;" sillyness.

--bb



More information about the Digitalmars-d mailing list