Thread overview
Simple D Questions (static if / #pragma / int[3][3])
Jul 26, 2012
Wes
Jul 26, 2012
bearophile
Jul 26, 2012
Simen Kjaeraas
Jul 26, 2012
Jacob Carlborg
July 26, 2012
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.
July 26, 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
July 26, 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
July 26, 2012
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