www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Stopping compile time execution

reply Russel Winder <russel winder.org.uk> writes:
I have a bit of code which is the definition of a class Preferences,
which has:

	this() {
		filePath =3D expandTilde(chainPath("~", ".config", "me-tv", "preferences.=
yml").array);

filePath is a member of the class. Now if I have a module level
instance (clearly not a good move but OK for now):

   public auto preferences =3D new Preferences;

then Dub using DMD on Debian Sid gives me:

/usr/include/dmd/phobos/std/path.d(3970,31): Error: getenv cannot be interp=
reted at compile time, because it has no available source code
/usr/include/dmd/phobos/std/path.d(4064,41):        called from here: expan=
dFromEnvironment(inputPath)
source/preferences.d(36,25):        called from here: expandTilde(array(cha=
inPath("~", ".config", "me-tv", "preferences.yml")))

What is the official way of getting preferences initialised at run time
and not compile time.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk
May 25 2018
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 26/05/2018 3:58 AM, Russel Winder wrote:
 I have a bit of code which is the definition of a class Preferences,
 which has:
 
 	this() {
 		filePath = expandTilde(chainPath("~", ".config", "me-tv",
"preferences.yml").array);
 
 filePath is a member of the class. Now if I have a module level
 instance (clearly not a good move but OK for now):
 
     public auto preferences = new Preferences;
 
 then Dub using DMD on Debian Sid gives me:
 
 /usr/include/dmd/phobos/std/path.d(3970,31): Error: getenv cannot be
interpreted at compile time, because it has no available source code
 /usr/include/dmd/phobos/std/path.d(4064,41):        called from here:
expandFromEnvironment(inputPath)
 source/preferences.d(36,25):        called from here:
expandTilde(array(chainPath("~", ".config", "me-tv", "preferences.yml")))
 
 What is the official way of getting preferences initialised at run time
 and not compile time.
Use a module constructor to initialize your global.
May 25 2018
parent Russel Winder <russel winder.org.uk> writes:
On Sat, 2018-05-26 at 04:00 +1200, rikki cattermole via Digitalmars-d-
learn wrote:
 [=E2=80=A6]
=20
 Use a module constructor to initialize your global.
Splendid idea! :-) I know, I should have thought of that. I note that if you click "Constructor" on https://dlang.org/spec/module .html you get taken to https://dlang.org/spec/class.html#Constructor which really does help describe the concept of a module constructor. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 25 2018