www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Another quirk with array initialization

More fun with static and dynamic arrays.

import std.stdio;

void main() {

   ubyte[]  dynamicArray   = [10, 20, 30, 40, 50];
   ubyte[5] staticArray    = [10, 20, 30, 40, 50];

}

Both gdc and dmd don't mind line 5 (dynamicArray) but trip on line 6:

arrayinit.d:6: Error: cannot implicitly convert expression 
([10,20,30,40,50]) of type int[5] to ubyte

I can only assume that the actual process of initializing the dynamic 
array on line 5 is a little different than that of the static array, but 
from a practical standpoint, should this be an error?

Yes, you can get around it with

ubyte[5] staticArray = [cast(ubyte)10, 20, 30, 40, 50];

but that seems a bit messy since the dynamic array initialization had no 
issues.

Any thoughts?

Joe
Jan 26 2007