Using mixin in array declarations

Marduk via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 19 01:58:13 PST 2016


On Saturday, 19 November 2016 at 09:49:48 UTC, rikki cattermole 
wrote:
> On 19/11/2016 10:46 PM, Marduk wrote:
>> In C one can do the following:
>>
>> # define N 10
>>
>> double M[N][N];
>>
>>
>> In D I would like to achieve the same result. I tried with:
>>
>> mixin("int N = 10;");
>>
>> double[N][N] M;
>>
>>
>> but the compiler (DMD) complained with Error: variable N 
>> cannot be read
>> at compile time.
>>
>> What am I doing wrong?
>
> enum N = 10;
>
> double[N][N] M;
>
> enum is a constant available for use at compile time, your int 
> there is a runtime variable not accessible at runtime.

Great! Thanks! I just understood why what I did did not work.


More information about the Digitalmars-d-learn mailing list