www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Transferring global variables from D to C

I'm testing how I could use globals in C, when using D as a library
and C as a frontend. So far it seems to be working well with some data
types, but not so much with a few others.

First of all, one obvious problem is strings, since they are a lot
different in D. I can transfer them with a wrapper function like this
(D code):

    immutable(char)* getString()
        return toStringz(MyString);

However, this gets quite inefficient with a lot of different strings,
and, if I understand how they work correctly, you also have to make
sure the pointer points to the string you want if it changes in D by
rerunning the function. That means that using a function instead of a
variable breaks the flow of the code in C. It would be a pretty good
solution if I could use a variable that automatically executes the
function, something like this (D code):

    string MyString="a";
    shared immutable(char)* a = () {return toStringz(MyString);};

But, of course, the compiler cannot compile it because it does not
evaluate at compile time. So are there any more solutions to this?

Struct packing can also be problematic, since if some members are not
of the same length, then any members below those will always return 0.
I have noticed that booleans, for example, translate to chars and not
ints, and while they do work as ints, any struct members below the
boolean will stop working. This is not mentioned in the documentation
at all.

I'm also not sure about how it would handle strings inside structs.
Are they actually just pointers in D, which means that they only take
the same amount of space in a struct as a regular char pointer?
Oct 01 2011