Passing a reference to a returned reference

Michael michael at toohuman.io
Fri Jul 6 14:36:18 UTC 2018


On Friday, 6 July 2018 at 14:11:42 UTC, Timoses wrote:
>
> This works for me:
>
>     auto create()
>     {
>         string[int] dict;
>         dict[2] = "hello";
>         return dict;
>     }
>
>     void modifyNoRef(string[int] m)
>     {
>          writeln("Address not ref: ", &m);
>          m[0] = "modified";
>     }
>
>     void modifyRef(ref string[int] m)
>     {
>          writeln("Address ref: ", &m);
>          m[1] = "modified";
>     }
>
>     unittest
>     {
>         auto r = create();
>         writeln("Address r: ", &r);
>         assert(r.keys.length == 1);
>         modifyNoRef(r);
>         assert(r.keys.length == 2);	
>         modifyRef(r);
>         assert(r.keys.length == 3);	
>     }
>
> So either with ref or not as parameter storage class the assoc. 
> array is modified. Note the address in the "ref" one is the 
> same as "r" in the unittest.
>
> [1]: https://dlang.org/spec/function.html#auto-ref-functions


I'm wondering, seeing as it works in the example given above, but 
not in my case, if this is a weird edge-case with setter member 
functions?

What I wanted to do was have the "Agent" object set its internal 
variable to point to the newly created associative array but 
instead it just seems to go right past the setter function.


More information about the Digitalmars-d-learn mailing list