www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Simple D Questions (static if / #pragma / int[3][3])

reply "Wes" <ffhighwind hotmail.com> writes:
1. Is int[3,3] the same in memory as int[3][3]?
I can't really tell based on the description.
http://dlang.org/arrays.html

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

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.
Jul 26 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Wes:

 1. Is int[3,3] the same in memory as int[3][3]?
 I can't really tell based on the description.
 http://dlang.org/arrays.html

Your first syntax is more like C#, not D. In D there are fixed-sized arrays like your second one, and it's rectangular, it's nine adjacent integers. A dynamic array of length 3 of a dynamic of 3 integers, created like this: new int[][](3, 3) is a very different data structure. It's a 2-word that contains length and pointer, the pointer points to 3 2-words that contain length and pointer, each one pointing to a 3-int chunk of memory (plus on each row a bit of bookeeping for the GC and to append new items).
 2. Is there a command such as #error or #warning in D?
 I assume this is maybe #pragma (msg, "error message") static
 assert(0);?

pragma(msg, "...") is for compile time messages. Maybe an alternative is to use: static assert(0, "..."); But in release mode it doesn't print the message.
 3. Can you use static if() {} with the {} block?

Yes, also the "else" part supports the braces {}. But keep in mind the braces don't create a scope for the names. Bye, bearophile
Jul 26 2012
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
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
Jul 26 2012
prev sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 26 Jul 2012 13:34:34 +0200, bearophile <bearophileHUGS lycos.com>  
wrote:

 static assert(0, "...");
 But in release mode it doesn't print the message.

Now that'd be something. :p -- Simen
Jul 26 2012