Changing behavior of associative array
Julian Fondren
julian.fondren at gmail.com
Sat Dec 16 23:45:03 UTC 2023
On Saturday, 16 December 2023 at 22:44:16 UTC, Dennis wrote:
> That's because `m[f] = 1` initializes the associative array to
> something non-null. If you pass a `null` AA to a function which
> adds things, the caller will still have a null pointers.
I've gotten this error in deployed Perl. Whenever the ceremony of
creating a data structure is reduced, people can lose sight of
when that happens.
Here's a less common gotcha:
```d
void main() {
import std.stdio : writeln;
int force_realloc(ref int[] seq) {
foreach (int i; 1 .. 1_000_000) {
seq ~= i;
}
return 1234;
}
int[] a = [4321];
writeln(a[0]); // 4321, of course
a[0] = force_realloc(a);
writeln(a[0]); // still 4321
}
```
The `a[0]` on the left of the assignment is decided early, and
then force_realloc() changes what the location should be.
More information about the Digitalmars-d-learn
mailing list