digitalmars.D.learn - Using mixin in array declarations
- Marduk (9/9) Nov 19 2016 In C one can do the following:
- rikki cattermole (5/14) Nov 19 2016 enum N = 10;
- Marduk (3/27) Nov 19 2016 Great! Thanks! I just understood why what I did did not work.
- Jonathan M Davis via Digitalmars-d-learn (20/29) Nov 19 2016 A string mixin literally puts the code there. So, doing
- Marduk (6/30) Nov 19 2016 Thank you very much for taking the time to write such a detailed
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
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
On Saturday, 19 November 2016 at 09:49:48 UTC, rikki cattermole wrote:On 19/11/2016 10:46 PM, Marduk wrote:Great! Thanks! I just understood why what I did did not work.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
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
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:Thank you very much for taking the time to write such a detailed explanation. The first part I had already figured out.[...]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 DavisString 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