How to initialise array of ubytes?

Xinok via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 30 12:08:37 PST 2014


On Saturday, 29 November 2014 at 20:47:09 UTC, Paul wrote:
> On Saturday, 29 November 2014 at 20:22:40 UTC, bearophile wrote:
>> This works:
>>
>> enum MAPSIZE = 3;
>> void main() {
>>    ubyte[MAPSIZE][MAPSIZE] map2 = 1;
>> }
>>
>>
>> This doesn't work:
>>
>> enum MAPSIZE = 3;
>> ubyte[MAPSIZE][MAPSIZE] map1 = ubyte(1);
>> void main() {}
>>
>> Why isn't this working?
>>
>
> I'm afraid I don't know. I would guess it's something to do 
> with trying to initialise the array in the global scope but 
> you've also changed the expression in the non-working example. 
> I don't have access to my machine at present so I can't 
> experiment!

More generally, it's because one is static (as in global / 
thread-local) and the other is not. Take the working example, put 
"static" in front of the declaration, and you'll get the same 
error.

There are different rules regarding the initialization of static 
and non-static variables. My guess, they're implemented as 
separate features in the compiler, so when vector operations were 
introduced, nobody thought to implement this feature as a way to 
initialize static arrays as well.


More information about the Digitalmars-d-learn mailing list