AHK V2: passing objects to non-byref param?

Discuss the future of the AutoHotkey language
SAbboushi
Posts: 116
Joined: 08 Dec 2014, 22:13

AHK V2: passing objects to non-byref param?

02 Jul 2018, 17:00

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

Code: [Select all] [Download] GeSHi © Codebox Plus

x := SomeFunction(SomeObject)

And
when you pass such a variable [an object] to a non-ByRef parameter
isn't it always passed "ByRef" since it is an object?
coffee
Posts: 65
Joined: 01 Apr 2017, 07:55

Re: AHK V2: passing objects to non-byref param?

02 Jul 2018, 23:32

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

Code: [Select all] [Download] GeSHi © Codebox Plus

x := SomeFunction(SomeObject)

And
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] [Expand] [Download] GeSHi © Codebox Plus

SAbboushi
Posts: 116
Joined: 08 Dec 2014, 22:13

Re: AHK V2: passing objects to non-byref param?

03 Jul 2018, 12:30

Thanks coffee - your script made it crystal clear. Much appreciated.

Wondering though since I'm still wrapping my mind around this: can anyone provide examples of why someone might want the function to mess with "the variable which the function's caller used" other than to release the object like in coffee's VERY NICELY DOCUMENTED script?
User avatar
jeeswg
Posts: 4785
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK V2: passing objects to non-byref param?

03 Jul 2018, 12:39

There are some points made here that may be relevant:
objects: passing objects ByRef to a function - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=46310
SAbboushi
Posts: 116
Joined: 08 Dec 2014, 22:13

Re: AHK V2: passing objects to non-byref param?

03 Jul 2018, 13:01

Thanks jeeswg. Good stuff!
lexikos
Posts: 6040
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AHK V2: passing objects to non-byref param?

07 Jul 2018, 01:08

In short: ByRef is typically used to provide additional return values. One would use ByRef if a new object should be constructed inside the function and returned by assigning it to the caller's variable.

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 3 guests