Importing, and visibility

Jarrett Billingsley jarrett.billingsley at gmail.com
Sun Oct 11 13:36:47 PDT 2009


On Sun, Oct 11, 2009 at 3:05 PM, Matt <webwraith at fastmail.fm> wrote:
> In C++ I see a lot of defining an enum, struct, or other values before #including another header which then uses these values. I understand why this works in C++, but does the same thing work in D? I'd assume not, since the import mechanism is a little more advanced than C++'s "copy this file into this location".

Not.. really. If module A imports module B, B does not see A's symbols at all.

Howeeeeever, it's possible to use mixins of various types to emulate
this. For instance, module B could define all of its members inside a
template, like so:

----
module B;

template BMembers()
{
    some declarations
    more declarations
}
----

Then in module A:

----
module A;
import B;

// define structs and stuff
mixin BMembers; // mixes in B's definitions into A, and they use A's symbols
----

You could also use the 'mixin(import("somefile"))' idiom at module
scope in A, but that's a bit hacky, and won't get you as decent error
messages.



More information about the Digitalmars-d mailing list