SAbboushi wrote:I've been looking at this paragraph for 2 months
, but think I'm still missing a distinction:
Because a variable only ever contains a reference to an object and not the object itself, when you pass such a variable to a non-ByRef parameter, what the function receives is a reference to the same object. This allows the function to modify the object, but it does not allow the function to modify the variable which the function's caller used, because the function only has a reference to the object, not the variable.
What variable is
the variable which the function's caller used
referring to? Is it referring to e.g. "x" in
when you pass such a variable [an object] to a non-ByRef parameter
isn't it always passed "ByRef" since it is an object?
The contents of the variable is the reference to the object, like what the doc says, the variable itself is not the object.
Think of the variable as a pointer to an object in this case.
If you pass it by value to a function, you are copying the object reference to the internal variable of the function.
The internal variable of the function in this case is not an alias to the external variable.
The object is always a reference. The variable is simply a container to something (number, string, object). They are not always a reference to each other, unless you choose to pass it byref.
If you do
i := object()
j := i
you are not copying the object, you are copying it's reference, unique identifier, name, place in memory, pointer.
j is not the same as i either, they are two different variables.
Code: Select all
tmp := object()
; tmp as a variable contains a reference to an object
; The object reference is a value that the tmp variable will hold.
; tmp is not the object.
; Think of tmp as pointer type.
; You can see the object address in the list vars window.
; Oversimplified like tmp := 0x42314
; tmp and the object are two different entities
; tmp simply holds a pointer to it, i.e content of tmp is an object reference.
; Similar to saying tmp := 10, content of tmp is an integer.
; Think of an object reference as an integer you can't see, something like 0x42314.
; tmp, please hold this object, 0x42314
; The easiest way to access the object after the expression that created is done evaluating is through a variable.
; (maybe you can do some fuckery with NumGet, but never tried).
; dot operator and brackets work like derefencing.
tmp := "hello"
function(tmp) ; Reference to object is copied to the parameter of the function
; i.e content of tmp is copied over to internal parameter of function.
; Say tmp holds an object which is: 0x42314, now copy this number to the internal variable.
msgbox(tmp " " tmp) ; hello world
; tmp as show above, still holds the reference to the object.
function2(tmp) ; Tmp is passed by reference.
; Its contents are not copied to the internal variable, tmp and param are emulated by autohotkey as being the same variable.
; Like a file system hard link in windows and linux.
msgbox(tmp " " tmp) ; Exception
; Because tmp was cleared inside function2.
; param contains a pointer to an object
; which was copied from tmp, emphasis on copied.
; Like 0x42314 was copied from tmp, so param now has 0x42314 as its content.
; tmp content is 0x42314
; param content is 0x42314
; param and tmp point to the same object, they have the same content
; but they are not an alias of each other.
; Not emulated as being the same variable.
param := "world"
param := ""
; Since param is not an alias of tmp
; blanking out param only clears the object reference inside the function
; param no longer as 0x42314 as its content.
; Outside variable tmp still has an object reference outside the function (still has 0x42314)
; therefore object remains in existence outside of the function.
; An object dies when all references to it, (no variable holds 0x42314 anymore), are cleared.
; param is an alias of tmp.
; Sort of like the same variable, just with a different name.
; param and tmp, by default, both contain an object reference, since they are emulated as the same variable.
; The object reference wasn't copied over. Param is now tmp and tmp contains the object reference.
; so tmp := 0x42314, param is tmp, so param content is 0x42314
; We set param before blanking it out, futile due to below.
param := "world"
param := ""
; Since param is an alias of tmp, i.e they are the same, if you blank param
; inside the function, it is equivalent to blanking out tmp (outside the function).
; So now the object reference is lost both inside the function and outside the function.
; 0x42314 is no longer the content.
; So the object is garbage collected.