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

bearophile bearophileHUGS at lycos.com
Thu Jul 26 04:34:34 PDT 2012


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


More information about the Digitalmars-d-learn mailing list