Member access of __gshared global object

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 6 06:24:12 PDT 2014


On Wednesday, 6 August 2014 at 04:14:51 UTC, Puming wrote:
> On Thursday, 31 July 2014 at 10:22:28 UTC, Marc Schütz wrote:
>> On Thursday, 31 July 2014 at 02:03:37 UTC, Puming wrote:
>>> 1. Are AAs reference type? if so, why does the compiler copy 
>>> it?
>>
>> This is probably your problem. They are reference types, but 
>> initially that reference is `null`. When you write:
>>
>>    auto cmds = CONFIG.commands;
>>
>> `cmds` contains a copy of the `null` value. On assignment, the 
>> actual AA is created, and assigned to `cmds`, but not back to 
>> `CONFIG.commands`. Try initializing the AA in your class 
>> constructor.
>
> I checked the code and could concur your suggestion.
>
> What I found strange were:
>
> 1. The only way that I can initialize it is to assign a value. 
> But I want to initialize an empty AA, is that possible?

I don't know whether there is a way currently. The obvious 
`CONFIG.commands = []` produces an error: "can't have associative 
array key of void". This is likely a restriction of the current 
implementation of AAs, which is known to be suboptimal and being 
reworked slowly.

>
> 2. CONFIG.commands is not `null` before initialized, it is 
> '[]'(when I writeln it).

That's just how writeln prints empty AAs. Try `writeln(cmds is 
null)`, it will print "true".

> But it still behave like what you described (except that it 
> won't produce an NullPointer Exception.

Because the runtime checks whether they are null, and allocates 
memory if necessary.


More information about the Digitalmars-d-learn mailing list