imports in functions
Jonathan M Davis
jmdavisProg at gmx.com
Sun Jun 12 16:14:13 PDT 2011
On 2011-06-12 15:08, Andrej Mitrovic wrote:
> This seems like a way to simulate namespaces in C++, right? I wouldn't
> know, but it looks similar to that 'using foo' trick.
C++ namespaces are completely different. Everything in a namespace has to be
referenced by its namespace explicitly unless you use using. So, without
using, you need to do stuff like
std::vector<int> v;
whereas with
using namespace std;
vector<int> v;
you don't need the std:: tag anymore. Using namespace affects _everything_
after it, which is why it's pretty much verboten in header files (otherwise it
would pollute the global namespace). And whether you have a using statement or
not, as soon as you #include a file, everything in it is visible in the
current file. Namespaces just segregate the names so that they don't clash.
D modules are very different. As soon as you import a module, everything in
that module is visible (though you can't use anything in it unless it's public
or the current module is in the same package and it's package or if you're
deriving from class in that module and the symbol in question is protected).
You don't have to use the module name when using _any_ of its symbols unless
names clash or you imported the module statically. It's kind of like you
automatically have a using statement all of the time, except that it's much
more sophisticated and handles name clashes much better.
By importing within a function, you're saying that only that function has
access to the symbols within the module being imported. As for as the rest of
the module is concerned, the imported module does not actually exist. You're
restricting what can see it.
So, I'm not quite sure what you mean by this simulating C++ namespaces. They
both deal with how symbols are brought into and viewable in the current file,
but they're very different.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list