Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D associative arrays - pass by value or pass by reference?

Say I have an associative array of pointers to structs to strings, where the struct is called Foo; the type will be Foo*[string]. Suppose I also have a function with the following signature: void bar (Foo*[string] baz). Will baz be passed to bar by value or by reference? I've not found any documentation about this, and I'm curious.

like image 852
Koz Ross Avatar asked Dec 07 '22 00:12

Koz Ross


2 Answers

By reference - any modifications to an existing AA will be observed outside of the function.

However, there is a corner case for when the associative array is null. In that case, initializing the AA by adding the first element will not be observed outside of the function. If the AA to be modified might be null, you should pass it by-ref.

like image 60
Vladimir Panteleev Avatar answered Dec 31 '22 16:12

Vladimir Panteleev


It's like in Java. It's pass-by-value, but the value is a reference.
So you can't change the reference itself, but you can modify the AA it refers to.

like image 35
user541686 Avatar answered Dec 31 '22 15:12

user541686