www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using mixin in array declarations

reply Marduk <mardukbp mac.com> writes:
In C one can do the following:



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?
Nov 19 2016
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 19/11/2016 10:46 PM, Marduk wrote:
 In C one can do the following:



 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.
Nov 19 2016
parent Marduk <mardukbp mac.com> writes:
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:



 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.
Nov 19 2016
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Saturday, November 19, 2016 09:46:08 Marduk via Digitalmars-d-learn 
wrote:
 In C one can do the following:



 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
Nov 19 2016
parent Marduk <mardukbp mac.com> writes:
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.
Nov 19 2016