UDF called in cmd syntax + force expr + object = ByRef?? Topic is solved

Discuss the future of the AutoHotkey language
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

UDF called in cmd syntax + force expr + object = ByRef??

22 Jul 2014, 13:00

Are objects passed to functions called in command syntax treated as ByRef? See sample below:

Code: Select all

obj := ["Hello World"] ;// store in variable
test % obj ;// works
test % obj := ["Hello World"] ;// works
test % ["Hello World"] ;// does not work

test(arg) { ;// arg is not ByRef
	MsgBox % IsObject(arg)
	MsgBox % arg[1]
}
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

22 Jul 2014, 20:56

Command syntax doesn't support objects. Each arg evaluates to a string or variable reference. % obj evaluates to a variable reference, so after all args are evaluated and the function call is being made, the variable's contents are copied into the arg parameter. % ["Hello World"] evaluates to an empty string.

Objects are always passed by reference, if at all. You don't store an object in a variable; you store a reference to an object. ByRef has nothing to do with objects, just variables. IsByRef(arg) returns false and assigning arg := 1 does not affect obj.
User avatar
joedf
Posts: 9000
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 00:17

As per documentation, this should suffice to free an object : obj := ""
But, is this always true? Would it better to do this? Object.SetCapacity(0)
Or with variables in general?
var := "" vs VarSetCapacity(var,0)
It seems to be the same...?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
just me
Posts: 9575
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 00:46

Object.SetCapacity(0)
MaxItems: The maximum number of key-value pairs the object should be able to contain before it must be automatically expanded. If less than the current number of key-value pairs, the object is shrunk to fit.
User avatar
joedf
Posts: 9000
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 02:41

I read that... So what about?
Object.SetCapacity(0) vs obj := ""
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
fincs
Posts: 527
Joined: 30 Sep 2013, 14:17
Location: Seville, Spain
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 05:01

obj := "" releases your reference to the object (which is unnecessary 99% of the time). If there are no more references left to the object, it is deleted; whereas obj.SetCapacity(0) wipes an object's contents; but does not free it completely.
fincs
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 - [My project list]
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 06:01

joedf, I can't see in what way your query is related to the topic of this thread.
fincs wrote:(which is unnecessary 99% of the time).
That seems like the wrong point to make. If the "99% of the time" only covers moments that you don't want the object to be freed, or cases where the variable is local and will be freed automatically, then sure. It is absolutely necessary to do obj := "" (or something equivalent) if the variable isn't going to be freed by some other means.
obj.SetCapacity(0) wipes an object's contents; but does not free it completely.
It looks like you did not read and understand the quoted documentation either.

SetCapacity(0) does not free the object nor wipe its contents. "Shrunk to fit" means that excess unused space is reclaimed by reallocating an internal array. All contents are preserved and the end result is an object whose capacity is equal to the number of key-value pairs it contains.
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 09:42

perhaps the docs can be updated to display this also:
lexikos wrote: SetCapacity(0) does not free the object nor wipe its contents. "Shrunk to fit" means that excess unused space is reclaimed by reallocating an internal array. All contents are preserved and the end result is an object whose capacity is equal to the number of key-value pairs it contains.

Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 10:04

lexikos wrote:Command syntax doesn't support objects.
lexikos wrote:Objects are always passed by reference
Thanks for clarifying. Are there plans to support objects for command syntax?
User avatar
joedf
Posts: 9000
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 12:56

I see... Thank you. Sorry if was a little offtopic.. :S
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

23 Jul 2014, 20:52

guest3456: I was going for brevity. Does anyone think "shrunk to fit" actually meant removing everything from the object? That doesn't make sense.

Coco: Eventually, yes.
just me
Posts: 9575
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: UDF called in cmd syntax + force expr + object = ByRef??

24 Jul 2014, 06:32

As a compromise:
... If less than the current number of key-value pairs, the object is shrunk to fit the current number.
Does it make sense?
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

Re: UDF called in cmd syntax + force expr + object = ByRef??

24 Jul 2014, 12:52

lexikos wrote:guest3456: I was going for brevity. Does anyone think "shrunk to fit" actually meant removing everything from the object? That doesn't make sense.
Whatever your intent, two separate users misunderstood. Clarity > Brevity imo. Pretty much whenever people have misunderstandings reading the docs, that is a time to improve the docs. This is for everyone's benefit, including yours. Less questions in the forum asking for your clarification

I do this constantly with my users. I don't have the patience to keep answering easy questions. So whenever someone misunderstands my docs, I update my docs and add more information so that the next user won't need to ask me.

Chris was fairly verbose in the original AHK docs and I think that was a good thing

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??

24 Jul 2014, 20:46

That's all beside the point. Did they misunderstand the documentation, or did they just make bad assumptions about what the method does without actually reading the documentation? It's completely pointless to cater the documentation to people that don't read it. In any case, I've updated it.
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: UDF called in cmd syntax + force expr + object = ByRef??  Topic is solved

28 Jul 2014, 08:08

I wrote:Command syntax doesn't support objects.
I take it back. As of v2.0-a049, Coco's code works.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 25 guests