trying to understand in, inout, and ref...

mark mark at qtrac.eu
Wed Jan 22 15:46:37 UTC 2020


On Wednesday, 22 January 2020 at 15:33:44 UTC, Adam D. Ruppe 
wrote:
> 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.

Thanks - that's exactly what I needed to know!

(I'm also very much enjoying your D Cookbook.)


More information about the Digitalmars-d-learn mailing list