There is a problem with the suggested syntax: it is not a natural extension of the existing assignment and comma operators. Currently the comma has lower precedence than assignment, meaning the grouping is like
(x), (y := MouseGetPos()).
I suppose it would need to be a special case for when an expression statement begins with
variable, variable ...
:=. Single-target assignments can be performed anywhere within an expression, but that couldn't be done without ambiguity for multiple targets. Lua allows multiple return values, but does not allow assignments within an expression (it uses
= for both assignment and comparison). That's probably how it is in most languages with multiple return values.
There is another way to handle it. JavaScript traditionally only supports single-return, and unlike AutoHotkey, has no ByRef. So to return multiple values, one must return an object. Objects are used so heavily in JavaScript that recent versions of JavaScript (or ECMAScript) have added
destructuring assignments. For example,
Code: Select all
var [x, y] = MouseGetPos(); // if it returns an array
var color = PixelGetColor(...MouseGetPos(),"Alt");
var {x, y} = MouseGetPos(); // if it returns an object with x and y properties
var color = PixelGetColor(x, y, "Alt"); // must pass them explicitly
// (but it's probably more conventional in JavaScript to pass an object).
How would you get just the second, third or fourth value? As it is now, you can omit the first two or three parameters if you just want the window or control HWND. In JavaScript...
The "rest" operator is used to get the rest of the values (and the same syntax is used for variadic functions):
Code: Select all
var [x, y, ...the_rest] = MouseGetPos();
AutoHotkey doesn't have a general var declaration keyword, and declaring variables with
global has potentially unwanted side effects. A statement beginning with
[...
] := would not be ambiguous, but something like
{...
} := is trickier because of the way blocks are handled. Destructuring assignments can be quite complex, since they can handle nested objects and arrays.
If it is possible to work destructuring assignments into the language, the same syntax could (instead) be used to deal with multiple return values. But in the end it's somewhat redundant, since we already have objects, arrays and ByRef parameters.
Also consider that depending on the language and context, sometimes a function call in a parameter list automatically results in multiple parameter values, and sometimes only the first value is used. In Lua only the first value is used, unless the function call is at the end of the parameter list.
Looking at the example
PixelGetColor(MouseGetPos(),"Alt"), it's hard to see that three parameters are being passed. The meaning of subsequent parameters depends on how many values MouseGetPos() returns. If it's a user-defined function, there might be multiple
return statements and each one could return a different number of values. Any function could do this, so any use of a function call directly in a parameter list becomes suspect.
On the other hand, if the function returns an array,
PixelGetColor(MouseGetPos()*,"Alt") at least shows that the number of parameters might vary based on the number of values returned.
Of course, I'm only now realizing a
major problem with both the idea and example: invalid assumptions would be made about the number of values a function returns. MouseGetPos has
four return values, so it would not be safe to use directly as a set of coordinates (
X, Y).