Why use .init for associative arrays?

Gilles G. schaouette at free.fr
Tue Jul 10 06:17:08 PDT 2007


Hello,
I am just wondering why we should use init for associative arrays of structs. See this code for example:
    struct S
    {
        int nb;
    }
    void main()
    {
        S[char[]] aa; // associative array of struct S
        aa["this_is_good"] = S.init;
        aa["this_is_good"].nb = 1;      // all right
        aa["this_is_an_error"].nb = 1; // runtime error, compiles fine...
    }

I can't see a use case for _not_ using init before accessing a new key, so why do we have to explicitely use S.init?
Why does this code compile fine (it gives an access violation error at runtime), shouldn't it always give an error?

Regards.

--
Gilles



More information about the Digitalmars-d-learn mailing list