http://d.puremagic.com/issues/show_bug.cgi?id=5279
Summary: Function-static associative arrays Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nob...@puremagic.com ReportedBy: bearophile_h...@eml.cc --- Comment #0 from bearophile_h...@eml.cc 2010-11-26 14:58:57 PST --- I'd like DMD to support the definition of static associative arrays, that become initialized only once at module start: void foo() { static string[string] map1 = ["bar" : "spam"]; static const string[string] map2 = ["bar" : "spam"]; static immutable string[string] map3 = ["bar" : "spam"]; } void main() {} That is similar to a static this() initialization of those maps, but with visibility limited to foo(). -------------------------- Just for reference this is how DMD 2.050 compiles various kinds of AAs literals, in all cases but fifth_function() the AA seems created again at each function call (I think there is already a bug report about the enum AAs, but I don't remember its number, please add it below if you remember it): string first_function(string k) { immutable string[string] map1 = ["bar" : "spam"]; return map1[k]; } string second_function(string k) { const string[string] map2 = ["bar" : "spam"]; return map2[k]; } string third_function(string k) { enum string[string] map3 = ["bar" : "spam"]; return map3[k]; } string fourth_function(string k) { static enum string[string] map4 = ["bar" : "spam"]; return map4[k]; } immutable string[string] map5; static this() { map5 = ["bar" : "spam"]; } string fifth_function(string k) { return map5[k]; } void main() {} Compiled with: DMD 2.050, -O -release -inline _D5test314first_functionFAyaZAya comdat L0: push EAX mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ mov ECX,offset FLAT:_D16TypeInfo_HAyayAa6__initZ push dword ptr 0Ch[ESP] push dword ptr 0Ch[ESP] push 8 push EAX push dword ptr FLAT:_DATA[01Ch] push dword ptr FLAT:_DATA[018h] push dword ptr FLAT:_DATA[0Ch] push dword ptr FLAT:_DATA[08h] push 1 push ECX call near ptr __d_assocarrayliteralT add ESP,018h push EAX call near ptr __aaGetRvalue mov EDX,4[EAX] mov EAX,[EAX] add ESP,014h pop ECX ret 8 _D5test315second_functionFAyaZAya comdat L0: push EAX mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ mov ECX,offset FLAT:_D17TypeInfo_HAyaxAya6__initZ push dword ptr 0Ch[ESP] push dword ptr 0Ch[ESP] push 8 push EAX push dword ptr FLAT:_DATA[01Ch] push dword ptr FLAT:_DATA[018h] push dword ptr FLAT:_DATA[0Ch] push dword ptr FLAT:_DATA[08h] push 1 push ECX call near ptr __d_assocarrayliteralT add ESP,018h push EAX call near ptr __aaGetRvalue mov EDX,4[EAX] mov EAX,[EAX] add ESP,014h pop ECX ret 8 _D5test314third_functionFAyaZAya comdat L0: push EAX mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ mov ECX,offset FLAT:_D16TypeInfo_HAyaAya6__initZ push dword ptr 0Ch[ESP] push dword ptr 0Ch[ESP] push 8 push EAX push dword ptr FLAT:_DATA[01Ch] push dword ptr FLAT:_DATA[018h] push dword ptr FLAT:_DATA[0Ch] push dword ptr FLAT:_DATA[08h] push 1 push ECX call near ptr __d_assocarrayliteralT add ESP,018h push EAX call near ptr __aaGetRvalue mov EDX,4[EAX] mov EAX,[EAX] add ESP,014h pop ECX ret 8 _D5test315fourth_functionFAyaZAya comdat L0: push EAX mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ mov ECX,offset FLAT:_D16TypeInfo_HAyaAya6__initZ push dword ptr 0Ch[ESP] push dword ptr 0Ch[ESP] push 8 push EAX push dword ptr FLAT:_DATA[01Ch] push dword ptr FLAT:_DATA[018h] push dword ptr FLAT:_DATA[0Ch] push dword ptr FLAT:_DATA[08h] push 1 push ECX call near ptr __d_assocarrayliteralT add ESP,018h push EAX call near ptr __aaGetRvalue mov EDX,4[EAX] mov EAX,[EAX] add ESP,014h pop ECX ret 8 _D5test314fifth_functionFAyaZAya comdat L0: push EAX mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ push dword ptr 0Ch[ESP] push dword ptr 0Ch[ESP] push 8 push EAX push dword ptr _D5test34map5yHAyaAa call near ptr __aaGetRvalue mov EDX,4[EAX] mov EAX,[EAX] add ESP,014h pop ECX ret 8 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------