Jump to content


Photo

[AHK_L 1.1.08.01] Ternary operand + variadic function


  • Please log in to reply
7 replies to this topic

#1 RaptorX

RaptorX
  • Members
  • 709 posts

Posted 27 September 2012 - 10:17 AM

Hi

I am not sure if this is actually a bug or if this is not really allowed but I tried the following:

fun1(1,2)
return

fun1(params*){
        msgbox true ; this msgbox works perfectly fine
	fun2(params.maxindex() ? params* : 0)
}

fun2(param=0,param2=0){
	msgbox % param " " param2 ; this one doesnt
}

; this didnt work either:
;fun2(param*){
	;msgbox % param[1] " " param[2]
;}

The code breaks there, no call to the second function is made (and if it is called the msgbox never reaches).
I am using a workaround at the moment but I am wondering if this is actually expected behaviour.

#2 RaptorX

RaptorX
  • Members
  • 709 posts

Posted 27 September 2012 - 10:24 AM

While Testing I also found that this has problems:

fun1() ; if empty second function breaks, if there is a parameter here it works totally fine
return

fun1(params*){
        msgbox true ; this msgbox works perfectly fine
	fun2(params*)
	
}

fun2(param){
	msgbox % param ; this one doesnt
}

--EDIT

Hmm I think I know what is going on now...

As the parameters are mandatory AHK cannot call the function but it is also not showing any errors so I was left wondering what was going on... I think an error here should be displayed (same as when you try to call a function without the needed params)

as for the 1st I have no idea what is up.

#3 geekdude

geekdude
  • Members
  • 321 posts

Posted 27 September 2012 - 12:30 PM

What is with the asterisk, I have not seen one used like this before. Could you please explain?

#4 RaptorX

RaptorX
  • Members
  • 709 posts

Posted 27 September 2012 - 01:25 PM

as the title explains that is for creating variadic functions... those are functions that do not have a specific amount of parameters... that means i can call them with as many parameters i want and then check param[1] for the first, param [2] for the second and so on. very useful :)

and yes it is valid ahk syntax... check manual file for more info

#5 Lexikos

Lexikos
  • Administrators
  • 8832 posts

Posted 27 September 2012 - 09:33 PM

fun2(params.maxindex() ? params* : 0)

Not supported. * must be applied to the function call, not the array, and therefore must be next to the parenthesis.

As the parameters are mandatory AHK cannot call the function but it is also not showing any errors so I was left wondering what was going on... I think an error here should be displayed (same as when you try to call a function without the needed params)

The behaviour is the same as for failed dynamic function calls and most other expression errors which occur at runtime. The error message indicating you have passed too few parameters is displayed before the script even starts; at that point, it is impossible to know how many parameters will be passed in a variadic call.

If the function cannot be called due to one of the reasons below, the evaluation of the expression containing the call stops silently and prematurely, which may lead to inconsistent results:
[*:1xirs6ej]Calling a nonexistent function, which can be avoided by using If IsFunc(VarContainingFuncName). Except for built-in functions, the called function's definition must exist explicitly in the script by means such as #Include or a non-dynamic call to a library function.
[*:1xirs6ej]Passing too few parameters, which can be avoided by checking IsFunc()'s return value (which is the number of mandatory parameters plus one). Note: In v1.0.48+, passing too many parameters is tolerated; each extra parameter is fully evaluated (including any calls to functions) and then discarded.

This will probably be changed in v2.

#6 RaptorX

RaptorX
  • Members
  • 709 posts

Posted 27 September 2012 - 10:26 PM

This will probably be changed in v2.


only the behavior for failed function calls or also allowing us to pass all parameters of a variadic function in a similar way as I was trying in the first example?

#7 Lexikos

Lexikos
  • Administrators
  • 8832 posts

Posted 28 September 2012 - 02:27 AM

Abnormal conditions during expression evaluation - including failed function calls and stack underflow caused by syntax errors (which aren't detected at load time) - will probably be changed to throw an exception/show an error message.

Btw, either of the following can be used:
fun2((params.maxindex() ? params : [0])*)  [color=#008000]; one variadic call[/color]
params.maxindex() ? fun2(params*) : fun2(0)  [color=#008000]; one variadic call, one non-variadic call[/color]
The second case is more efficient.

#8 guest3456

guest3456
  • Members
  • 1324 posts

Posted 28 September 2012 - 03:59 AM

Btw, either of the following can be used:

fun2((params.maxindex() ? params : [0])*)  [color=#008000]; one variadic call[/color]
params.maxindex() ? fun2(params*) : fun2(0)  [color=#008000]; one variadic call, one non-variadic call[/color]
The second case is more efficient.


much cleaner to read too