digitalmars.D.learn - Associative array for array of classes
- clayasaurus (13/13) Apr 26 2006 Ok, for basic types I know I can use
- Derek Parnell (10/39) Apr 26 2006 Try this ...
- clayasaurus (2/9) Apr 26 2006 Thanks :) That seems to do the trick.
-
Stewart Gordon
(5/15)
Apr 27 2006
- Alberto Simon (5/20) Apr 27 2006 You should use '(("3" in assoc) == null)' instead of '(assoc["3"] is nul...
- Sean Kelly (4/29) Apr 27 2006 The correct use of AAs is so intuitive I'm amazed anyone ever gets it
-
Stewart Gordon
(12/15)
Apr 28 2006
Ok, for basic types I know I can use int[char[]] assoc; assoc["3"] = 3; And for classes I can do. Class[char[]] assoc; assoc["3"] = new Class; Now, lets say I want to test if assoc["3"] has already been initialized before I allocate it, but testing it with if (assoc["3"] is null) assoc["3"] = new Class; I get a seg fault. Any other way to do this? File attached to better illustrate my problem. Thanks. ~ Clay
Apr 26 2006
On Wed, 26 Apr 2006 23:49:07 -0500, clayasaurus wrote:import std.stdio; int main() { char[] i = "3"; int[char[]] assoc; assoc[i] = 3; Class[char[]] assoc2; // doesn't work if (assoc2[i] is null) assoc2[i] = new Class; // works assoc2[i] = new Class; writefln("hi"); return 0; } class Class { public: this(){} int x,y; }Try this ... if (!(i in assoc2)) assoc2[i] = new Class; -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 27/04/2006 3:09:47 PM
Apr 26 2006
Derek Parnell wrote:On Wed, 26 Apr 2006 23:49:07 -0500, clayasaurus wrote: Try this ... if (!(i in assoc2)) assoc2[i] = new Class;Thanks :) That seems to do the trick.
Apr 26 2006
clayasaurus wrote: <snip>Class[char[]] assoc; assoc["3"] = new Class; Now, lets say I want to test if assoc["3"] has already been initialized before I allocate it, but testing it with if (assoc["3"] is null) assoc["3"] = new Class; I get a seg fault.<snip> That's strange. It should be an ArrayBoundsError. Stewart.
Apr 27 2006
You should use '(("3" in assoc) == null)' instead of '(assoc["3"] is null)' Regards, Alberto Simon "Stewart Gordon" <smjg_1998 yahoo.com> escribió en el mensaje news:e2qh6d$1lfk$2 digitaldaemon.com...clayasaurus wrote: <snip>Class[char[]] assoc; assoc["3"] = new Class; Now, lets say I want to test if assoc["3"] has already been initialized before I allocate it, but testing it with if (assoc["3"] is null) assoc["3"] = new Class; I get a seg fault.<snip> That's strange. It should be an ArrayBoundsError. Stewart.
Apr 27 2006
The correct use of AAs is so intuitive I'm amazed anyone ever gets it wrong. It's times like this when I wish I'd been more vocal about actually liking the old syntax :-p Alberto Simon wrote:You should use '(("3" in assoc) == null)' instead of '(assoc["3"] is null)' Regards, Alberto Simon "Stewart Gordon" <smjg_1998 yahoo.com> escribió en el mensaje news:e2qh6d$1lfk$2 digitaldaemon.com...clayasaurus wrote: <snip>Class[char[]] assoc; assoc["3"] = new Class; Now, lets say I want to test if assoc["3"] has already been initialized before I allocate it, but testing it with if (assoc["3"] is null) assoc["3"] = new Class; I get a seg fault.<snip> That's strange. It should be an ArrayBoundsError. Stewart.
Apr 27 2006
Sean Kelly wrote:The correct use of AAs is so intuitive I'm amazed anyone ever gets it wrong. It's times like this when I wish I'd been more vocal about actually liking the old syntax :-p<snip top of upside-down reply> What old syntax? The in operator has always been the correct way to check whether an AA contains a given key. Looking it up and comparing it against valuetype.init was never a general solution. And if ("3" in assoc) or if (!("3" in assoc)) still works just the same. [F'up back to d.D.learn] Stewart.
Apr 28 2006