Confusion over what types have value vs reference semantics
rikki cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Sep 11 08:26:10 PDT 2016
On 12/09/2016 3:15 AM, Neurone wrote:
> Hi,
>
> Are there universal rules that I can apply to determine what types have
> reference or value semantics? I know that for the basic primitive C
> types (int, bool, etc) has value semantics.
>
> In particular, I'm still trying to understand stack vs GC-managed
> arrays, and slices.
>
> Finally, I have an associative array, and I want to pass it into a
> function. The function is only reading data. Would putting ref on in
> function parameter pass it by reference?
Ok two questions here:
1) What constitutes value vs reference passing
2) Allocation location
So basically you have two "locations" where something can be stored, on
the stack or the heap.
Now I put it in quotes because they are both in RAM somewhere so it
doesn't matter too much. The only difference is only a subset of the
stack is readily allocatable at any single function call.
So in regarding to what gets passed by value, well that is simple.
If it is a class, you're passing a few pointers as your reference.
All primal types including pointers are passed as values.
If you use ref, you turn a type into a pointer auto magically (a
location in the stack most likely).
Just so you're aware, arrays are slices in D (excluding static arrays).
They are simply a pointer + length.
So I suppose you can think of references and slices as a container of
sorts for other values which get passed in.
More information about the Digitalmars-d-learn
mailing list