Using mixin in array declarations
Marduk via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Nov 19 11:47:35 PST 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:
>> [...]
>
> 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.
More information about the Digitalmars-d-learn
mailing list