trying to understand in, inout, and ref...
Adam D. Ruppe
destructionator at gmail.com
Wed Jan 22 15:33:44 UTC 2020
On Wednesday, 22 January 2020 at 15:26:06 UTC, mark wrote:
> I've done this but my impression from the docs is that passing
> slices and associative arrays is already done by reference so
> these aren't needed?
They are pointers passed by value.
If you're familiar with C, think of passing
struct Array {
size_t length;
element* ptr;
}
The elements are passed by ref there; they aren't copied to the
function and any changes to them will be visible outside.
BUT if you change the length of it or reallocate it in any way
those changes are NOT seen outside.
So with AAs and slices, if you just want to work with existing
elements, no need for ref. But if you are going to do any kind of
resizing - adding or removing elements - ref is likely what you
want.
More information about the Digitalmars-d-learn
mailing list