www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Conditional compilation inside an array initializer

reply Johan Engelen <j j.nl> writes:
Is it possible to do conditional compilation inside an array 
initializer? Something like this:
int[] inttable =
[
     1,
     4,
version(smth)  {  // <--- does not compile
     5,
     6,
}
     8,
   1345
];

(real world case: 
https://github.com/ldc-developers/ldc/blob/merge-2.069/ddmd/idgen.d#L279)

If it is not possible, what alternative solution would you use?

Thanks!
Jan 18
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 19 January 2016 at 00:33:21 UTC, Johan Engelen wrote:
 Is it possible to do conditional compilation inside an array 
 initializer?
No, but you might break it up: enum inttable_1 = [1,4]; version(smth) enum inttable_middle = [5,6]; else enum inttable_middle = []; enum inttable_2 = [8, 1345]; int[] inttable = inttable_1 ~ inttable_middle ~ inttable_2;
Jan 18
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 01/18/2016 04:38 PM, Adam D. Ruppe wrote:
 On Tuesday, 19 January 2016 at 00:33:21 UTC, Johan Engelen wrote:
 Is it possible to do conditional compilation inside an array initializer?
No, but you might break it up: enum inttable_1 = [1,4]; version(smth) enum inttable_middle = [5,6]; else enum inttable_middle = []; enum inttable_2 = [8, 1345]; int[] inttable = inttable_1 ~ inttable_middle ~ inttable_2;
I was writing something similar: int[] table; // Alternatively, consider 'shared static this()' static this() { const tens = [ 10, 20 ]; const hundreds = [ 100, 200 ]; const thousands = [ 1000, 2000 ]; table ~= tens; version (smth) { table ~= hundreds; } table ~= thousands; } version = smth; void main() { import std.stdio; writeln(table); } Ali
Jan 18