Why doesn't AutoHotkey support default options in the middle of the options list?

In AutoHotkey, you can leave the arguments in the middle of a built-in function call as follows:

MsgBox, 4,, Blah MouseGetPos,,, MouseWin 

You can also create functions with additional parameters Γ  la C ++:

 Foobar(baz, blah="something") { MsgBox baz=%baz%, blah=%blah% } 

However, docs say that when creating a function, you cannot have parameters other than the default parameters by default. Attempting to do this will result in an interpreter error saying that the first argument other than the default after the default argument requires a default.

Why? What is wrong, calling it that?

 Foobar(baz, blah="something", blivet) { MsgBox baz=%baz%, blah=%blah%, blivet=%blivet% } Foobar("cat",,"dog") 

Is there a way to create functions with additional parameters in the middle?

+4
source share
2 answers

Two options ... (workarounds ...)

1. Place all parameters with default values ​​on the right ...

 Foobar(baz, blivet, blah="something") { MsgBox baz=%baz%, blah=%blah%, blivet=%blivet% } Foobar("cat","dog") 

2. Define an "empty" default value ...

 Foobar(baz, blah="something", blivet="") { MsgBox baz=%baz%, blah=%blah%, blivet=%blivet% } Foobar("cat",,"dog") 

I can’t say why this is so, but there is no other way ... (unless you change autohotkey: P)
I think the built-in functions do not work this way.

+1
source

When you omit a parameter in an inline command (and not in a function), what you really do is pass an empty string. There are differences between the two cases:

  • Defining a function with an optional parameter, followed by the required parameters.
  • Invokes a command with an optional optional parameter, followed by optional parameters that were not omitted.

With MsgBox and MouseGetPos, all parameters are optional.

AutoHotkey 1.1 allows you to use custom functions:

 Foobar(1,, 3) Foobar(baz, blah="something", blivet="") { MsgBox baz=%baz%, blah=%blah%, blivet=%blivet% } 

This is possible only when the default value of the parameter is known (i.e. not when the function is called dynamically).


Allow to skip the middle parameter

If you do not want to change the order of the parameters or make two of the three optional, you can "juggle" a little:

 Foobar("baz", "blivet") Foobar("baz", "blah", "blivet") Foobar(baz, p2, p3="omitted") { blah := p3="omitted" ? "default" : p2 ; optional blivet := p3="omitted" ? p2 : p3 ; required MsgBox baz=%baz%, blah=%blah%, blivet=%blivet% } 

Thus, a function always requires at least two parameters, and you can really omit the parameter in the middle when the function calls . However, you need to reserve a (string or numeric) value to indicate that the parameter is omitted. This can be avoided with AutoHotkey 1.1 using the variational function :

 Foobar("baz", "blivet") Foobar("baz", "blah", "blivet") Foobar(baz, p2, p3*) { blah := p3.MaxIndex() ? p2 : "default" ; optional blivet := p3.MaxIndex() ? p3[1] : p2 ; required MsgBox baz=%baz%, blah=%blah%, blivet=%blivet% } 

Alternatively, the function can be declared as Foobar(baz, p*) , and you can base your legend on p.MaxIndex() (the number of additional parameters), but in this case only the first parameter is required.

+7
source

Source: https://habr.com/ru/post/1437328/


All Articles