digitalmars.D.learn - Associative array for array of classes
- clayasaurus <clayasaurus gmail.com> Apr 26 2006
- Derek Parnell <derek psych.ward> Apr 26 2006
- clayasaurus <clayasaurus gmail.com> Apr 26 2006
- Stewart Gordon <smjg_1998 yahoo.com> Apr 27 2006
- "Alberto Simon" <lugaidster gmail.com> Apr 27 2006
- Sean Kelly <sean f4.ca> Apr 27 2006
- Stewart Gordon <smjg_1998 yahoo.com> Apr 28 2006
Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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.
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.
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.
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
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









clayasaurus <clayasaurus gmail.com> 