Simple D Questions (static if / #pragma / int[3][3])

Jacob Carlborg doob at me.com
Thu Jul 26 04:44:38 PDT 2012


On 2012-07-26 13:22, Wes wrote:

> 2. Is there a command such as #error or #warning in D?
> I assume this is maybe #pragma (msg, "error message") static
> assert(0);?

D doesn't really have any of these, but you can emulate them. You can use:

pragma(msg, "warning: message")

To emulate a warning. This will be printed during compilation. For 
#error you can use:

static assert(0, "error message");

This will halt the compilation with the given message.

> 3. Can you use static if() {} with the {} block?
> None of the examples had it. I know it's because of scope
> issues... but I'd hate to have to write static if(x) 5 times in a
> row for 5 variables.

Yes, you can use {} after a static-if:

static if (a) {}
else {}

Note that even though you use {} it will not introduce a new scope:

import std.stdio;

static if (true) { int a = 3; }

void main () { writeln(a); }

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list