digitalmars.D.learn - defining in a module symbols for export
- spir (41/41) Nov 22 2010 Hello,
- bearophile (4/5) Nov 22 2010 I suggest to declare global variables and then initialize them in a "sta...
- spir (12/20) Nov 22 2010 modules's top level. for instancz:
- =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= (11/43) Nov 22 2010 To initialize at runtime:
- spir (21/81) Nov 23 2010 tand-alone modules -- for code structuration and reusability. Then, impo...
- Jesse Phillips (17/34) Nov 22 2010 Others have answered how to initialize variables, but haven't explained ...
- spir (11/58) Nov 23 2010 he compilation error. A simple example of what you are doing wrong is:
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 (=3D=3D list of patterns) at = the module's top-level. I have nothing against this, since the module is de= dicated to this anyway -- but dmd refuses. More generally, I find myself unable to define structured objects at a modu= les's top level. for instancz: =3D=3D=3D mod.d =3D=3D import std.stdio; struct S { int i; void speak() {writeln("i: ",this.i);} } =3D=3D=3D __trials__.d =3D=3D=3D import mod; auto s =3D S(); s.speak(); s.i =3D 1; writeln(s.i); void main () { } =3D=3D=3D compilation =3D=3D=3D __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 th= e module. Is there another solution? I take the opportunity to ask whether it is possible to define which symbol= s are to be _ex_ported. Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 22 2010
spir:More generally, I find myself unable to define structured objects at a modules's top level. for instancz:I suggest to declare global variables and then initialize them in a "static this() {...}". But generally I suggest you to minimize the number of global variables. Bye, bearophile
Nov 22 2010
On Mon, 22 Nov 2010 15:11:29 -0500 bearophile <bearophileHUGS lycos.com> wrote:spir: =20modules's top level. for instancz:More generally, I find myself unable to define structured objects at a ==20 I suggest to declare global variables and then initialize them in a "stat=ic this() {...}". But generally I suggest you to minimize the number of glo= bal variables.=20 Bye, bearophileYes, I would generally agree (I normally have about nothing at module tople= vel), but then how can they be exported? A parser module is precisely here = to define symbols for export... How do you guys write that? denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 22 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.comTo 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 :-)
Nov 22 2010
On Mon, 22 Nov 2010 22:17:46 +0100 Pelle M=C3=A5nsson <pelle.mansson gmail.com> wrote:On 11/22/2010 08:18 PM, spir wrote:tand-alone modules -- for code structuration and reusability. Then, import = them from apps that need them.Hello, Let us say I have a parsing library. Now, I want to define parsers in s=at the module's top-level. I have nothing against this, since the module i= s dedicated to this anyway -- but dmd refuses.Is there another way than defining the parser (=3D=3D list of patterns)=modules's top level. for instancz:More generally, I find myself unable to define structured objects at a =nk, and run it. But this does not match my use case: I need to export symbo= ls (patterns) defined there; so, i guess, they must be defined at the top o= f the module. Is there another solution?=3D=3D=3D mod.d =3D=3D import std.stdio; struct S { int i; void speak() {writeln("i: ",this.i);} } =3D=3D=3D __trials__.d =3D=3D=3D import mod; auto s =3D S(); s.speak(); s.i =3D 1; writeln(s.i); void main () { } =3D=3D=3D compilation =3D=3D=3D __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, li=mbols are to be _ex_ported.I take the opportunity to ask whether it is possible to define which sy=Thank you very much! Unfortunately, a parser is typically a bunch a definitions. I cannot expect= the user (first, myself) to write a func for every little setting needed. I could write setters on classes that require the user to set attributes, b= ut as shown in the example, method calls are also refused. Where is the issue, actually? Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.comDenis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com=20 To initialize at runtime: =20 S s; static this() { s.i =3D 1; } =20 to initialize at compile time: =20 S getS() { S s; s.i =3D 1; return s; } S s =3D getS(); =20 Use public/private to control which symbols are availible.=20 Unfortunately, this doesn't really work at the moment :-)
Nov 23 2010
spir Wrote:=== 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 () { }Others have answered how to initialize variables, but haven't explained the compilation error. A simple example of what you are doing wrong is: import std.stdio; writeln("foo"); void main () { } How are you to execute writeln()? You probably wouldn't try this under normal conditions, but is what you are trying to do. You can place it in a static this constructor: import std.stdio; static this() { writeln("foo"); } void main () { } Note you can move the calls into main and it compiles fine: void main () { s.speak(); s.i = 1; writeln(s.i); }
Nov 22 2010
On Mon, 22 Nov 2010 16:20:07 -0500 Jesse Phillips <jessekphillips+D gmail.com> wrote:spir Wrote: =20 =20he compilation error. A simple example of what you are doing wrong is:=3D=3D=3D mod.d =3D=3D import std.stdio; =20 struct S { int i; void speak() {writeln("i: ",this.i);} } =3D=3D=3D __trials__.d =3D=3D=3D import mod; =20 auto s =3D S(); s.speak(); s.i =3D 1; writeln(s.i); =20 void main () { }=20 Others have answered how to initialize variables, but haven't explained t==20 import std.stdio; =20 writeln("foo"); =20 void main () { } =20 How are you to execute writeln()? You probably wouldn't try this under no=rmal conditions, but is what you are trying to do. You can place it in a st= atic this constructor:=20 import std.stdio; =20 static this() { writeln("foo"); } =20 void main () { } =20 Note you can move the calls into main and it compiles fine: =20 void main () { s.speak(); s.i =3D 1; writeln(s.i); } =20Right. Seems I may put the whole parser definition into static this(). I'll= try it. Will work if symbols defined in there are available for export. Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 23 2010