[Issue 2179] import inside class works but is not in spec
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jul 14 13:35:03 PDT 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2179
------- Comment #4 from wbaxter at gmail.com 2008-07-14 15:35 -------
(In reply to comment #2)
> (In reply to comment #1)
> As it is, I'm not seeing many good applications.
It solves the classic problem of how to ensure that you have access to all the
symbols that a mixin depends upon.
If in mix.d you have a simple mixin:
import std.stdio;
template() Mix {
void blarf() { writefln("blarf"); }
}
and then you mixin that in a class in another file, that other file also needs
to be sure to have writefln accessible. You could make std.stdio a public
import of Mix (ick) or selectively make public aliases of all the symbols you
use in Mix (easy to forget to update). But both those are foiled if the user
decides to use selective import of just Mix.
So what putting the import inside the mixin (and thereby inside the class where
it gets mixed in) does is allow you to make sure the dependencies of the mixin
always get carried around with the mixin.
I suppose it would also be possible to make aliases to all needed symbols
inside the template, but again it's easy to forget one and the list could get
quite long. Much easier to just import the modules the mixin needs inside the
mixin.
--
More information about the Digitalmars-d-bugs
mailing list