Changing behavior of associative array

2023-12-16 Thread Kevin Bailey via Digitalmars-d-learn
Perhaps someone can help solve this mystery. I have a sample program that adds structs to an associative array (as keys) in a subroutine. The program passes the array by reference as expected (below). My real program does the same thing and yet the associative array is passed by value. Thus a

Re: Changing behavior of associative array

2023-12-16 Thread kdevel via Digitalmars-d-learn
On Saturday, 16 December 2023 at 20:04:54 UTC, Kevin Bailey wrote: I've added a TON of writeln's to confirm this. The *instant* I made the parameter "ref", the real program starts acting as expected, and the print-outs show the correct results. If you comment out this line ``` //m[f] = 1;

Re: Changing behavior of associative array

2023-12-16 Thread Dennis via Digitalmars-d-learn
On Saturday, 16 December 2023 at 21:30:55 UTC, kdevel wrote: If you comment out this line ``` //m[f] = 1; ``` in your main function of your posted code you can catch up with your real programm insofar as you now need a ref parameter here, too. That's because `m[f] = 1` initializes the a

Re: Changing behavior of associative array

2023-12-16 Thread Julian Fondren via Digitalmars-d-learn
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

Re: Changing behavior of associative array

2023-12-16 Thread Kevin Bailey via Digitalmars-d-learn
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. You can initialize a non-null empty AA like this

Re: Changing behavior of associative array

2023-12-16 Thread Julian Fondren via Digitalmars-d-learn
On Sunday, 17 December 2023 at 00:10:56 UTC, Kevin Bailey wrote: instead it seems like 'm' is a pointer to a std::map, that is initialized on use if null. (I think it's that latter part that gives the illusion of being already initialized.) Yes: https://dlang.org/spec/hash-map.html#constructi