Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 19, 2016 Using mixin in array declarations | ||||
---|---|---|---|---|
| ||||
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? |
November 19, 2016 Re: Using mixin in array declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marduk | 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.
|
November 19, 2016 Re: Using mixin in array declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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.
|
November 19, 2016 Re: Using mixin in array declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marduk | On Saturday, November 19, 2016 09:46:08 Marduk via Digitalmars-d-learn 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?
A string mixin literally puts the code there. So, doing
mixin("int n = 10");
double[n][n] m;
is identical to
int n = 10;
double[n][n] m;
except that you made the compile do the extra work of converting the string mixin to the code. String mixins really only become valuable when you start doing string manipulation rather than simply using string literals. If you want a compile-time constant, then use the enum keyword. e.g.
enum n = 10;
double[n][n] m;
And if you want the value of n to be calculated instead of being fixed, then you can even do something like
enum n = calcN();
double[n][n] m;
so long as calcN can be run at compile time.
- Jonathan M Davis
|
November 19, 2016 Re: Using mixin in array declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Saturday, 19 November 2016 at 13:57:26 UTC, Jonathan M Davis wrote: > On Saturday, November 19, 2016 09:46:08 Marduk via Digitalmars-d-learn wrote: >> [...] > > A string mixin literally puts the code there. So, doing > > mixin("int n = 10"); > double[n][n] m; > > is identical to > > int n = 10; > double[n][n] m; > > except that you made the compile do the extra work of converting the string mixin to the code. String mixins really only become valuable when you start doing string manipulation rather than simply using string literals. If you want a compile-time constant, then use the enum keyword. e.g. > > enum n = 10; > double[n][n] m; > > And if you want the value of n to be calculated instead of being fixed, then you can even do something like > > enum n = calcN(); > double[n][n] m; > > so long as calcN can be run at compile time. > > - Jonathan M Davis Thank you very much for taking the time to write such a detailed explanation. The first part I had already figured out. > String mixins really only become valuable when you start doing string manipulation rather than simply using string literals. Yes. I saw some examples in the docs. The last part is very interesting. |
Copyright © 1999-2021 by the D Language Foundation