defining in a module symbols for export
Pelle Månsson
pelle.mansson at gmail.com
Mon Nov 22 13:17:46 PST 2010
On 11/22/2010 08:18 PM, spir wrote:
> Hello,
>
> Let us say I have a parsing library. Now, I want to define parsers in stand-alone modules -- for code structuration and reusability. Then, import them from apps that need them.
> Is there another way than defining the parser (== list of patterns) at the module's top-level. I have nothing against this, since the module is dedicated to this anyway -- but dmd refuses.
> More generally, I find myself unable to define structured objects at a modules's top level. for instancz:
>
> === mod.d ==
> import std.stdio;
>
> struct S {
> int i;
> void speak() {writeln("i: ",this.i);}
> }
> === __trials__.d ===
> import mod;
>
> auto s = S();
> s.speak();
> s.i = 1;
> writeln(s.i);
>
> void main () {
> }
>
> === compilation ===
> __trials__.d(19): no identifier for declarator s.speak
> __trials__.d(20): no identifier for declarator s.i
> __trials__.d(21): found '.' when expecting ')'
> __trials__.d(21): semicolon expected, not 'i'
> __trials__.d(21): no identifier for declarator i
> __trials__.d(21): semicolon expected, not ')'
> __trials__.d(21): Declaration expected, not ')'
>
> Why does dmd refuse? If I put the code in a function, I can compile, link, and run it. But this does not match my use case: I need to export symbols (patterns) defined there; so, i guess, they must be defined at the top of the module. Is there another solution?
>
> I take the opportunity to ask whether it is possible to define which symbols are to be _ex_ported.
>
>
> Denis
> -- -- -- -- -- -- --
> vit esse estrany ☣
>
> spir.wikidot.com
>
To initialize at runtime:
S s;
static this() {
s.i = 1;
}
to initialize at compile time:
S getS() { S s; s.i = 1; return s; }
S s = getS();
Use public/private to control which symbols are availible.
Unfortunately, this doesn't really work at the moment :-)
More information about the Digitalmars-d-learn
mailing list