Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 27, 2006 Associative array for array of classes | ||||
---|---|---|---|---|
| ||||
Attachments: | 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 Re: Associative array for array of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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 Re: Associative array for array of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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 Re: Associative array for array of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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 Re: Associative array for array of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | 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 Re: Associative array for array of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alberto Simon | 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 Re: Associative array for array of classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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.
|
Copyright © 1999-2021 by the D Language Foundation