Pass an expression as function argument

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
dotm
Posts: 7
Joined: 02 Jan 2017, 14:37

Pass an expression as function argument

02 Jan 2017, 18:11

Basically I would like to provide a drop-in replacement for

Code: Select all

MsgBox, %foo%, %bar%
occurrences with something like

Code: Select all

Log(...)

...

Log(val) {
	global LOG_LEVEL
	If LOG_LEVEL
		MsgBox, %val%
}
How can an expression be provided as function argument?

I've had to do this previously with ugly workaround

Code: Select all

output = %foo%, %bar%
Log(output)
FWIW, AHK version is 1.1.08.
dotm
Posts: 7
Joined: 02 Jan 2017, 14:37

Re: Pass an expression as function argument

02 Jan 2017, 19:05

Xtra wrote:

Code: Select all

Log(foo . "`," . bar)
Thank you. For now I'm trying to avoid this if possible. Is there no way to save % expressions in their current state? I've intended to just replace MsgBox/ToolTip strings with Log via regexps.
User avatar
Xtra
Posts: 2751
Joined: 02 Oct 2015, 12:15

Re: Pass an expression as function argument

02 Jan 2017, 19:37

Code: Select all

Log(foo,bar)

...

Log(val1,val2) {
	global LOG_LEVEL
	If LOG_LEVEL
		MsgBox, %val1% %val2%
}
guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: Pass an expression as function argument

02 Jan 2017, 19:47

dotm wrote:
Xtra wrote:

Code: Select all

Log(foo . "`," . bar)
Thank you. For now I'm trying to avoid this if possible. Is there no way to save % expressions in their current state? I've intended to just replace MsgBox/ToolTip strings with Log via regexps.
commands such as MsgBox don't accept expressions unless you specifically FORCE an expression. your current code doesn't use expressions, it uses literal text in which variables are de-referenced via %%s

Code: Select all

MsgBox, %foo% and %bar%          ; literal text
MsgBox, % foo . " and " . bar       ; forced expression parameter
function parameters always expect expressions, and therefore they don't need the single % to force the expression:

Code: Select all

Log(foo . " and " . bar)
if you want a complete drop in replacement, you'd have to use AHK v2 where you can call user-defined functions with Command syntax. for example:

Code: Select all

Log, %foo% and %bar%

Log(param)
{
   MsgBox, %param%
}


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 81 guests