Thread overview
Associative array for array of classes
Apr 27, 2006
clayasaurus
Apr 27, 2006
Derek Parnell
Apr 27, 2006
clayasaurus
Apr 27, 2006
Stewart Gordon
Apr 28, 2006
Alberto Simon
Apr 28, 2006
Sean Kelly
Apr 28, 2006
Stewart Gordon
April 27, 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


April 27, 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
April 27, 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.
April 27, 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.
April 28, 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.


April 28, 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. 
> 
> 
April 28, 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.