www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialising global associative array

reply "Paul" <paul example.com> writes:
I need a globally accessible AA of type string[string] and find 
that it won't compile unless I use the static this(){} method as 
described in this thread:

http://forum.dlang.org/thread/owhfdwrpfuiehzpiuqux forum.dlang.org#post-mailman.1522.1346449072.31962.digitalmars-d-learn:40puremagic.com

I understand the concept of this() insofar as returning a 
reference to an object but what is 'this' in the context referred 
to? A reference to the module??

It doesn't seem very intuitive (to me at least!) especially given 
that strings are immutable anyway.

Paul
Jun 06 2015
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2015-06-06 20:18, Paul wrote:
 I need a globally accessible AA of type string[string] and find that it
 won't compile unless I use the static this(){} method as described in
 this thread:

 http://forum.dlang.org/thread/owhfdwrpfuiehzpiuqux forum.dlang.org#post-mailman.1522.1346449072.31962.digitalmars-d-learn:40puremagic.com


 I understand the concept of this() insofar as returning a reference to
 an object but what is 'this' in the context referred to? A reference to
 the module??
"static this" is a module constructor. It's run before the "main" function is run. -- /Jacob Carlborg
Jun 06 2015
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Saturday, June 06, 2015 18:18:07 Paul via Digitalmars-d-learn wrote:
 I need a globally accessible AA of type string[string] and find
 that it won't compile unless I use the static this(){} method as
 described in this thread:

 http://forum.dlang.org/thread/owhfdwrpfuiehzpiuqux forum.dlang.org#post-mailman.1522.1346449072.31962.digitalmars-d-learn:40puremagic.com

 I understand the concept of this() insofar as returning a
 reference to an object but what is 'this' in the context referred
 to? A reference to the module??

 It doesn't seem very intuitive (to me at least!) especially given
 that strings are immutable anyway.
It's talking about static constructors, which are functions that run before main rather than being constructors which are tied to specific objects. If you search for "static constructor" on this page, you'll find some documentation on them: http://dlang.org/class.html Another place to read up on them would be Ali's book: http://ddili.org/ders/d.en/modules.html - Jonathan M Davis
Jun 06 2015
parent reply "Paul" <paul example.com> writes:
On Saturday, 6 June 2015 at 19:07:58 UTC, Jonathan M Davis wrote:
 On Saturday, June 06, 2015 18:18:07 Paul via 
 Digitalmars-d-learn wrote:
 I need a globally accessible AA of type string[string] and find
 that it won't compile unless I use the static this(){} method 
 as
 described in this thread:

 http://forum.dlang.org/thread/owhfdwrpfuiehzpiuqux forum.dlang.org#post-mailman.1522.1346449072.31962.digitalmars-d-learn:40puremagic.com

 I understand the concept of this() insofar as returning a
 reference to an object but what is 'this' in the context 
 referred
 to? A reference to the module??

 It doesn't seem very intuitive (to me at least!) especially 
 given
 that strings are immutable anyway.
It's talking about static constructors, which are functions that run before main rather than being constructors which are tied to specific objects. If you search for "static constructor" on this page, you'll find some documentation on them: http://dlang.org/class.html Another place to read up on them would be Ali's book: http://ddili.org/ders/d.en/modules.html - Jonathan M Davis
Thanks both, that makes sense. However, I now get an 'undefined identifier' error when compiling and trying to access the AA from a function within the same module. static this() { string[string] tagWords = [ "DIST" : "Distance", ..... } ... if(tag in tagWords) { retVal.tag = tagWords[tag]; <---- 'Error: undefined identifier tagWords' ... According to Ali's book that looks like a linker message but I am compiling like so: dmd -w oftest *.d Do I need to qualify the name in some way or give DMD some more info?
Jun 06 2015
parent reply "anonymous" <anonymous example.com> writes:
On Saturday, 6 June 2015 at 20:30:50 UTC, Paul wrote:
 However, I now get an 'undefined identifier' error when 
 compiling and trying to access the AA from a function within 
 the same module.

 static this()
 {
   string[string] tagWords = [ 	"DIST" : "Distance", .....
 }

 ...

   if(tag in tagWords)
   {
     retVal.tag = tagWords[tag]; <---- 'Error: undefined 
 identifier tagWords'
     ...
You have to declare tagWords in module scope, but initialize it in the static constructor: string[string] tagWords; static this() { tagWords = [ "DIST" : "Distance", ..... }
 According to Ali's book that looks like a linker message
Nope, that's not a linker message. "undefined identifier" = compiler "undefined reference" = linker
Jun 06 2015
parent "Paul" <paul example.com> writes:
On Saturday, 6 June 2015 at 20:39:14 UTC, anonymous wrote:
 On Saturday, 6 June 2015 at 20:30:50 UTC, Paul wrote:
 However, I now get an 'undefined identifier' error when 
 compiling and trying to access the AA from a function within 
 the same module.

 static this()
 {
  string[string] tagWords = [ 	"DIST" : "Distance", .....
 }

 ...

  if(tag in tagWords)
  {
    retVal.tag = tagWords[tag]; <---- 'Error: undefined 
 identifier tagWords'
    ...
You have to declare tagWords in module scope, but initialize it in the static constructor: string[string] tagWords; static this() { tagWords = [ "DIST" : "Distance", ..... }
 According to Ali's book that looks like a linker message
Nope, that's not a linker message. "undefined identifier" = compiler "undefined reference" = linker
Doh! Thank you anonymous person :D
Jun 06 2015