Thread overview | ||||||
---|---|---|---|---|---|---|
|
October 02, 2007 Associative Array Non-null initialization | ||||
---|---|---|---|---|
| ||||
Hi, I want to declare an AA such that it's starting length is 0, yet, it is not null. For instance: 1 string[int] aa; 2 assert (aa.length == 0); 3 assert (aa is null); 4 aa[100] = ""; 5 aa.remove(100); 6 assert (aa.length == 0); 7 assert (aa !is null); The above runs without error, as it should. Notice that assertions 3 and 7 are opposite. However, is there anything shorter/more efficient that I can do (preferably on line 1) which would generate an AA which satisfies the assertions 6 and 7? This appears to function in the same way: string[int] aa = [100:""]; aa.remove(100); assert (aa.length == 0); assert (aa !is null); Thank you. |
October 02, 2007 Re: Associative Array Non-null initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyle G. | Reply to Kyle G., > Hi, > > I want to declare an AA such that it's starting length is 0, yet, it > is not null. > > For instance: > > 1 string[int] aa; > 2 assert (aa.length == 0); > 3 assert (aa is null); > 4 aa[100] = ""; > 5 aa.remove(100); > 6 assert (aa.length == 0); > 7 assert (aa !is null); > The above runs without error, as it should. Notice that assertions 3 > and 7 are opposite. > > However, is there anything shorter/more efficient that I can do > (preferably on line 1) which would generate an AA which satisfies the > assertions 6 and 7? > > This appears to function in the same way: > > string[int] aa = [100:""]; > aa.remove(100); > assert (aa.length == 0); > assert (aa !is null); > Thank you. > its a pitty AA.remove is of type void. If it wasn't then this would work: ([int.init:char[].init].remove(int.init)) |
October 02, 2007 Re: Associative Array Non-null initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Reply to Kyle G.,
>
>> Hi,
>>
>> I want to declare an AA such that it's starting length is 0, yet, it
>> is not null.
>>
>> For instance:
>>
>> 1 string[int] aa;
>> 2 assert (aa.length == 0);
>> 3 assert (aa is null);
>> 4 aa[100] = "";
>> 5 aa.remove(100);
>> 6 assert (aa.length == 0);
>> 7 assert (aa !is null);
>> The above runs without error, as it should. Notice that assertions 3
>> and 7 are opposite.
>>
>> However, is there anything shorter/more efficient that I can do
>> (preferably on line 1) which would generate an AA which satisfies the
>> assertions 6 and 7?
>>
>> This appears to function in the same way:
>>
>> string[int] aa = [100:""];
>> aa.remove(100);
>> assert (aa.length == 0);
>> assert (aa !is null);
>> Thank you.
>>
>
> its a pitty AA.remove is of type void. If it wasn't then this would work:
>
> ([int.init:char[].init].remove(int.init))
>
>
I suppose you could define a remove alternative:
V[K] bremove(V,K) (ref V[K] ar, K k) {
ar.remove(k);
return ar;
}
|
October 04, 2007 Re: Associative Array Non-null initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyle G. | "Kyle G." wrote
> Hi,
>
> I want to declare an AA such that it's starting length is 0, yet, it is not null.
The question I would ask you is: why?
I understand that it is weird, and I agree it should be consistent, but using the length property is consistent. Why not just ignore the comparison to null and only use the length check?
IMO, arrays should not be comparable to null, since they are internally structures.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation