[a101] UDFnObj .Call() excess params should throw maybe? Topic is solved

Discuss the future of the AutoHotkey language
swagfag
Posts: 2930
Joined: 11 Jan 2017, 17:59

[a101] UDFnObj .Call() excess params should throw maybe?

06 May 2019, 11:34

im expecting this to throw but it doesnt. where are the rest of the params going

Code: Select all

class UDFnObj
{
	Call(arg1, arg2) {
		MsgBox arg1 '`n' arg2
	}
}

fn := new UDFnObj()

%fn%('ok', 'still ok', 'these', 'should', 'throw')
compare:

Code: Select all

fn('ok', 'still ok', 'these', 'should', 'throw', 'and they do')

fn(arg1, arg2) {
	MsgBox arg1 '`n' arg2
}
throws Too many params passed to function
Helgef
Posts: 3929
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a101] UDFnObj .Call() excess params should throw maybe?

06 May 2019, 14:12

Passing too many parameters is tolerated; each extra parameter is fully evaluated (including any calls to functions) and then discarded.
src
swagfag
Posts: 2930
Joined: 11 Jan 2017, 17:59

Re: [a101] UDFnObj .Call() excess params should throw maybe?

06 May 2019, 15:24

i didnt see this
is that a limitation of the current implementation or the actual intended behavior? i cant imagine the latter being the case. still think it should throw
lexikos
Posts: 6613
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [a101] UDFnObj .Call() excess params should throw maybe?

06 May 2019, 22:23

It was intended behavior. Before variadic calls, there was no way to call a function with a variable number of parameters, so dynamic calls were permitted to pass more than the function accepts.

It is still useful for calling callbacks where the callback author isn't required to define all parameters (like OnMessage, OnEvent, etc.) and the function object is not required to implement MaxParams accurately or at all. (I will probably add this to BoundFunc, but the function object is not required to be a standard one.)

Perhaps the default should be to throw, with some new syntax to permit surplus parameters. Catching the exception won't necessarily work as intended, since it could be thrown from within the function during an initially successful call.
swagfag
Posts: 2930
Joined: 11 Jan 2017, 17:59

Re: [a101] UDFnObj .Call() excess params should throw maybe?

07 May 2019, 08:47

yes i was thinking "well, if i wanted to have it work with varargs, id have defined it as such, a la Call(arg1, arg2, theRest*) {", but now i see it behaves that way for historical reasons
thank u for ur input, both of u
lexikos
Posts: 6613
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [a101] UDFnObj .Call() excess params should throw maybe?

07 May 2019, 21:35

The point is not to "work with varargs". Varargs would just be a workaround for the restriction placed by parameter count validation.

Normally, there is one function definition and multiple callers. The author of the function decides which parameter it accepts and what they mean, so passing more is not meaningful. The author of the function can decide parameters are optional, and give them default values.

With callbacks, there is one call site and many function definitions. The author of the library which contains the call site decides what the parameters mean. When calling the callback, there is no way to say "these parameters are not required to be handled". Instead (with full parameter count validation), the callback author is required define every parameter, or to accept a variable number of parameters even though it is known in advance exactly how many the function will use.

In some languages, the callback would need to match the predefined signature exactly.
User avatar
nnnik
Posts: 4257
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [a101] UDFnObj .Call() excess params should throw maybe?

08 May 2019, 01:31

Which is preferable since an unmatching parameter count is among the safest indicators for errerneous code. The convenience of callbacks seems completely irrelevant when put into that context.
Recommends AHK Studio
lexikos
Posts: 6613
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [a101] UDFnObj .Call() excess params should throw maybe?  Topic is solved

12 May 2019, 20:20

Suggestion implemented in v2.0-a103.

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: Bing [Bot] and 5 guests