namespace (for export)
Simen kjaeraas
simen.kjaras at gmail.com
Thu Nov 25 10:28:13 PST 2010
spir <denis.spir at gmail.com> wrote:
> In the general case, defining symbols requires kinds of statement (but I
> don't see what you mean by "statement" as for me an assignemnt is the
> archetype of a statement, so how can you define anything?).
http://digitalmars.com/d/2.0/declaration.html
A variable declaration is a declaration, not a statement. Note also that
initialization is included, so int i = 4; works.
> Anyway, if I put definitions in a func, as you propose, then how can I
> have the symbols available for export? The answer seems to be: declare
> them first as globals. Right, but then we get this double definition
> issue (not that serious, just annoying); it's just a useless hack.
> What I don't understand is why the language refuses the same exact
> definition at the toplevel that it accepts in a func.
Because run time code can not be run at arbitrary points in a D program
like you'd expect from a more dynamic language like python.
Note however, that some code may be evaluated at compile time:
http://www.digitalmars.com/d/2.0/function.html#interpretation
> You seems to take it for granted that I cannot write:
> auto myExportedSymbol = new T();
> ...
> myExportedSymbol.i = 1;
> but for me there is no obvious reason why D refuses that, and accepts
> T myExportedSymbol;
> static this () {
> auto myExportedSymbol = new T();
> ...
> myExportedSymbol.i = 1;
> }
> It's the same thing, from my point of view: when I import the module, I
> get the symbol correctly defined. I don't get why a module needs a
> constructor: why else has it a top-level?
The reason for this is partly historical (C does it this way), but more
importantly, practical. This way it is obvious what code will be executed
at which time, and code is not as easily 'hidden' between type and
variable declarations.
>> All a module constructor is for is initializing global variables at
>> runtime.
>
> Precising runtime to import time, that's exactly what I'm looking for.
> How can one do it without static this?
You can't.
--
Simen
More information about the Digitalmars-d-learn
mailing list