Thread overview
GC & module constructor
Nov 09, 2002
Lloyd Dupont
Nov 09, 2002
Walter
Nov 09, 2002
Lloyd Dupont
Nov 09, 2002
Patrick Down
Nov 09, 2002
Sean L. Palmer
Nov 13, 2002
Walter
Nov 09, 2002
Lloyd Dupont
November 09, 2002
I have 2 question:

1./ about Module
---------------------
I try
#-------------
extern (C) char* getenv(char*);
char[] tostring(char* cstring)
{
    int n = strlen(cstring);
    return cstring[0 .. n];
}
char[] PATH = tostring(getenv("PATH"));
#-------------
and it keep failing dmd outputting:
> f.d(11): non-constant expression tostring(getenv("PATH"))

I also tried (property)

char[] PATH() { return tostring(getenv("PATH")); }
....
and later printf(PATH ~ " It worked!\n");
but it doesn't work either.
how could I initialise my module variable ??

2./ about Garbage Collection:
------------------------------------
I am a bit puzzled in you "wc" example you don't initialiase gc at all.. is
it garbage collected ?
I wonder if garbage collection is enabled by default or not, as your (other)
example "D for win32" you explicitly initialise it...

should I initialise explicitly or not ?


3./ yes :-D
-------------
why don't you link directly with DLL as gcc do ?
I found it's a nice feature and it ease life so much...


November 09, 2002
"Lloyd Dupont" <lloyd@galador.net> wrote in message news:aqi0ur$19h$1@digitaldaemon.com...
> I have 2 question:
>
> 1./ about Module
> ---------------------
> I try
> #-------------
> extern (C) char* getenv(char*);
> char[] tostring(char* cstring)
> {
>     int n = strlen(cstring);
>     return cstring[0 .. n];
> }
> char[] PATH = tostring(getenv("PATH"));
> #-------------
> and it keep failing dmd outputting:
> > f.d(11): non-constant expression tostring(getenv("PATH"))

No dynamic initializers, try instead:
    char[] PATH;
    PATH = tostring ...

>
> I also tried (property)
>
> char[] PATH() { return tostring(getenv("PATH")); }
> ....
> and later printf(PATH ~ " It worked!\n");
> but it doesn't work either.
> how could I initialise my module variable ??
>
> 2./ about Garbage Collection:
> ------------------------------------
> I am a bit puzzled in you "wc" example you don't initialiase gc at all..
is
> it garbage collected ?
> I wonder if garbage collection is enabled by default or not, as your
(other)
> example "D for win32" you explicitly initialise it...
>
> should I initialise explicitly or not ?

The runtime library automatically initializes the GC for you.

> 3./ yes :-D
> -------------
> why don't you link directly with DLL as gcc do ?
> I found it's a nice feature and it ease life so much...

I don't know what you mean here - what DLL?


November 09, 2002
> No dynamic initializers, try instead:
>     char[] PATH;
>     PATH = tostring ...
I meant, what about that
# --------------
import string;

extern (C) char* getenv(char*);
char[] tostring(char* cstring)
{
    int n = strlen(cstring);
    return cstring[0 .. n];
}

char[] PATH;
PATH = tostring(getenv("PATH"));

// dmd f.d -o f.exe && f
void main (char[][] args)
{
    printf(PATH ~ " -- HEHE --");
}
# --------------
this doesn't compile.
of course I could imagine PATH being a function (PATH()). using a lazy
value.
but I wonder if there is away to have an init() function, called only once
by module,
which could initialiaze my variables....

> The runtime library automatically initializes the GC for you.
ah, cool !...
so why do you initialize it in you "D for Win32" examples ?
is it because the programs start in WinMain, in which case the runtime is
not initialized ?
(and idem in DllMain for DLL ?)

> > 3./ yes :-D
> > -------------
> > why don't you link directly with DLL as gcc do ?
> > I found it's a nice feature and it ease life so much...
> I don't know what you mean here - what DLL?
well here is an example
#-- dll.c
#include <stdio.h>
#define DLLOBJECT __declspec(dllexport)

DLLOBJECT void writeln(char* s)
{
     printf("%s\n", s);
}
# -------
compile with "gcc -shared -o dll.dll dll.c"
#-- a.c
__declspec(dllimport) void writeln(char* s);

main()
{
    writeln("Hello");
}
# ---
compile with "gcc -o a.exe a.c dll.dll"

you see, I link directly with the DLL, no def file, no lib file, only 1 step, simple..




November 09, 2002
"Lloyd Dupont" <lloyd@galador.net> wrote in news:aqiioa$jk3$1@digitaldaemon.com:


> but I wonder if there is away to have an init() function, called only
> once by module,
> which could initialiaze my variables....

http://www.digitalmars.com/d/module.html

Static module constuctors and destructors

char[] PATH;

static this()
{
    	PATH = tostring(getenv("PATH"));
}
November 09, 2002
But I'd rather be able to do it in the initializer.  Static variable initializers should effectively be automatically transferred to the module's static ctor.  Same as member variable initializers effectively occurring when the ctor(s) are called.

I figure just about anything you can do with assignment you should be able to do in an initializer.  Why not?  It's convenient.

Sean

"Patrick Down" <pat@codemoon.com> wrote in message news:Xns92C15DCA09ED9patcodemooncom@63.105.9.61...
> "Lloyd Dupont" <lloyd@galador.net> wrote in news:aqiioa$jk3$1@digitaldaemon.com:
>
>
> > but I wonder if there is away to have an init() function, called only
> > once by module,
> > which could initialiaze my variables....
>
> http://www.digitalmars.com/d/module.html
>
> Static module constuctors and destructors
>
> char[] PATH;
>
> static this()
> {
>     PATH = tostring(getenv("PATH"));
> }


November 09, 2002
ha, oh, .. thanks..
I read that but didn't guess what was the static constructor,
thanks to outlined it with your example :-)

"Patrick Down" <pat@codemoon.com> a écrit dans le message de news: Xns92C15DCA09ED9patcodemooncom@63.105.9.61...
> "Lloyd Dupont" <lloyd@galador.net> wrote in news:aqiioa$jk3$1@digitaldaemon.com:
> > but I wonder if there is away to have an init() function, called only
> > once by module,
> > which could initialiaze my variables....
> http://www.digitalmars.com/d/module.html
>
> Static module constuctors and destructors
>
> char[] PATH;
>
> static this()
> {
>     PATH = tostring(getenv("PATH"));
> }


November 13, 2002
"Sean L. Palmer" <seanpalmer@directvinternet.com> wrote in message news:aqjt49$28nr$1@digitaldaemon.com...
> But I'd rather be able to do it in the initializer.  Static variable initializers should effectively be automatically transferred to the
module's
> static ctor.  Same as member variable initializers effectively occurring
> when the ctor(s) are called.
>
> I figure just about anything you can do with assignment you should be able to do in an initializer.  Why not?  It's convenient.

It is convenient, but I wanted to be able to distinguish between what is computed at runtime and what is at compile time. The way it is now makes it pretty obvious.