When I put tmp1 outside the main loop, it compiles fine and gives the expected output. When tmp1 is put inside the main loop, the compiler seems to get stuck in a loop?

//immutable int[] tmp1 = [1, 2]; // compiles

void main()
{       
        immutable int[] tmp1 = [1, 2]; // does not compile
        
        int[tmp1.length] tmp2 = tmp1;
        
        tmp2[] += 1;



Generally the int[tmp1.length] syntax tries to define a fixed-sized array, but currently in D there are no VariableLengthArrays of C99, there are only dynamic arrays and fixed-sized arrays, so the size must be known at compile-time. But when tmp1 is defined inside the main, it is a dynamic array, so its length isn't a compile-time known value. This explains why it doesn't compile if tmp1 is defined inside the main().

The large number of the same error message is small a compiler diagnostic bug, that should be reported in bugzilla.

When tmp1 is defined globally, dmd is doing something different, in some way it sees global immutables almost as enums... I don't know if this is present in D specs.

Bye,
bearophile

Reply via email to