Member access of __gshared global object
    Daniel Kozak via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Wed Jul 30 23:27:42 PDT 2014
    
    
  
V Thu, 31 Jul 2014 02:03:35 +0000
Puming via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
napsáno:
> Hi,
> 
> I'm writing this global Config class, with an AA member:
> 
> ```d
> module my.config;
> 
> class Config
> {
>      Command[string] commands;
> }
> 
> __gshared Config CONFIG;
> ```
> 
> and initialize it in another module:
> 
> ```d
> module my.app;
> 
> import my.config;
> 
> void main()
> {
>    CONFIG = new Config();
>    CONFIG.commands["bye"] = new Command(...); // add commands
> }
> ```
> 
> This is OK. But when I use a local variable to hold the commands 
> AA:
> 
> ```
> auto cmds = CONFIG.commands;
> cmds["list"] = new Command(...);
> ```
> 
> The command "list" is not added.
> 
> I guess what happened here was that `cmds` is a threadlocal 
> variable, so the compiler somehow copied the CONFIG.commands.
> 
> My questions are:
> 
> 1. Are AAs reference type? if so, why does the compiler copy it?
> 2. How do I reference a member of __gshared global objects?
can you post code somewhere? I try it and it works for me.
module main;
import std.stdio;
import config;
void main(string[] args)
{
	CONFIG = new Config();
	CONFIG.commands["bye"] = "yep";
	auto cmds = CONFIG.commands;
	cmds["list"] = "smt";
	writeln(CONFIG.commands);
	// Lets the user press <Return> before program returns
	stdin.readln();
}
module config;
class Config
{
	string[string] commands;
	this()
	{
		// Constructor code
	}
}
__gshared Config CONFIG;
    
    
More information about the Digitalmars-d-learn
mailing list