Associative Array c'tor
Bahman Movaqar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jul 11 20:38:44 PDT 2016
On 07/11/2016 07:15 PM, Ali Çehreli wrote:
> Both AAs and slices behave like reference types even when passed by
> value: When a function adds an element, the argument sees that element
> as well. This is not the case when the argument is an empty (more
> correctly, null) AA or slice:
>
> void foo(string[int] aa) {
> aa[1] = "one";
> }
>
> void main() {
> string[int] a;
> foo(a);
> assert(a is null);
> // The last result would be different if 'a' were not null
> // before calling 'foo'.
>
> string[int] b;
> b[0] = "zero";
> foo(b);
> assert(b[0] == "zero");
> assert(b[1] == "one");
> }
Now I understand.
This is tricky --could introduce hard to find bugs. Is there anyway to
make sure it doesn't happen? Such as giving the AA a default empty
value on the declaration line --like `string[int] a = []`?
> P.P.S. There is std.algorithm.fold, which works with range chaining
> (unlike reduce, which was designed before ranges):
>
> https://dlang.org/phobos/std_algorithm_iteration.html#.fold
`fold` definitely feels more natural.
Thanks.
--
Bahman
More information about the Digitalmars-d-learn
mailing list