Code: Select all
debug(s,line=A_Linenumber)
Got a compiler error stating that this is an illegal default value. Why don't you allow that?
Any expession that can be evaluated at the time of the funtion call could be a default value for a parameter.
Code: Select all
debug(s,line=A_Linenumber)
Code: Select all
debug(s,A_Linenumber)
...
...
debug(s,line)
{}
Of course I can pass the parameter manually. But the idea of default values for parameters is that I can save myself typing them. The compiler can insert them automatically, if they are omitted, it has all necessary information. The code could be built just as if the default parameter has been passed explicitly.
Reading this, not understanding how it would save on typing. This is because a built-in variable can always be included in the body of the function, thus serve the same use as if a default parameter. From a functional programming perspective, I do understand how some purist might want the function to act only on the parameters its been given, but what built-in variables will return is expected and is known. Be that as it may, AutoHotkey's optional parameters usually are good for other use cases, but sometimes we can't have everything. Of course it's up to the developers to decide.braunbaer wrote: ↑22 Dec 2020, 15:18Of course I can pass the parameter manually. But the idea of default values for parameters is that I can save myself typing them. The compiler can insert them automatically, if they are omitted, it has all necessary information. The code could be built just as if the default parameter has been passed explicitly.
I don't see the reason for this restriction...
Code: Select all
S := "The script's name is:"
Example1(S, A_ScriptName)
Example2(S)
Return
Example1(S, X)
{
MsgBox,, Result, % S A_Space X
}
Example2(S)
{
X := A_ScriptName
MsgBox,, Result, % S A_Space X
}
There is at least one situation where it would save VERY MUCH typing.
Code: Select all
my_func(file_name = "%A_ScriptName%") {
return A_ScriptDir . "\" . file_name . ".ahk"
}
MsgBox, % my_func() ; Use default parameter.
MsgBox, % my_func("my_script") ; Use custom parameter.
Good catch Delta, I overlooked the double-dereference and forced expression type thingy. Consequently, the below works too.Delta Pythagorean wrote: ↑25 Dec 2020, 20:27Technically you can do this already but it's very bad practice and only works for a handful of variables (that I can be bothered to try):Code: Select all
my_func(file_name = "%A_ScriptName%") { return A_ScriptDir . "\" . file_name . ".ahk" }
Code: Select all
S := "The script's name is:"
Example3(S)
Y := Example4(S)
MsgBox % Y
Return
Example3(S, X = "A_ScriptName")
{
MsgBox,, Result, % S A_Space %X%
}
Example4(S, X = "A_ScriptName")
{
Y := % S A_Space %X%
Return Y
}
braunbaer wrote:... I would like to declare debug(string, linenumber=A_Linenumber). This would save me writing ",A_Linenumber" everytime I use this function call (and that happens often during debugging). ...
Code: Select all
debug(string, linenumber := A_Linenumber) {
...
}
That's an interesting idea, but it does not solve my problem. Because of course A_linenumber, when passed as string, will only be evaluated in the body of the function and will not contain the linenumber of the line where the function is calledDelta Pythagorean wrote: ↑25 Dec 2020, 20:27Technically you can do this already but it's very bad practice and only works for a handful of variables (that I can be bothered to try):Code: Select all
my_func(file_name = "%A_ScriptName%") { return A_ScriptDir . "\" . file_name . ".ahk" } MsgBox, % my_func() ; Use default parameter. MsgBox, % my_func("my_script") ; Use custom parameter.
True, A_linenumber in particular is a tough one. You could save a few key strokes by doing below, though it's not quite what you want.
Code: Select all
X := "A_Linenumber"
Test =
(
This
Is
Just
Filler
)
MsgBox % Example5(Test, %X%)
Return
Example5(S, Y :="")
{
Z := % S "`n" Y
Return Z
}
Code: Select all
box(x, y := x)
Example,Exception wrote: If What is omitted, it defaults to the name of the current function. Otherwise it can be a string or a negative offset from the top of the call stack. For example, a value of -1 sets Exception.What to the current function, and Exception.Line and Exception.File to the line and file which called it.
Code: Select all
f(l:=""){
if (l=="")
l:=exception("",-1).line
msgbox % l
}
f()
f()
Code: Select all
l:=exception("",-1).line
outputtodebugfile("The error occured here,l)"
Code: Select all
outputtodebugfile("The error occured here",exception("",-1).line)
That's another good one to know, thanks again. Good to learn something new every day.Helgef wrote: ↑09 Jan 2021, 09:45If parameters could have arbitrary expressions as their default value, I think it would be more useful if the expression was evaluated in function scope rather than call site...
For the particular case of A_LineNumber, you can use the Exception() function...
Cheers.
That would obviously not be intended behaviour, and so should not be added to the documentation.
Code: Select all
outputtodebugfile("The error occured here",exception("",-1).line)
outputtodebugfile(a,s){
MsgBox % s
}
Users browsing this forum: No registered users and 26 guests