ManaUser wrote:
known as a kludge…
Here is another one, versions of which have been posted to the Forum already:
Code:
; . . .
Switch(12)
Switch(Case) {
GoTo % IsLabel("Case-" Case) ? "Case-" Case : "Case-Default"
Case-A:
MsgBox Case-A
Return
Case-12:
MsgBox Case-12
Return
Case-Default:
MsgBox Default
Return
}
; . . .
MsgBox Next Statements
Here a function definition imitates a block. This one has two extra code lines, not necessary with true Switch/Case commands, but the problem with these workarounds is that you have difficulties if you need more of them in one script. The case labels are now true labels, which have to be unique in the whole script, that is, you need to keep track of unused name variants:
Code:
SwitchX(11)
SwitchX(Case) {
GoTo % IsLabel("CaseX-" Case) ? "CaseX-" Case : "CaseX-Default"
CaseX-A:
MsgBox Case-A
Return
CaseX-12:
MsgBox Case-12
Return
CaseX-Default:
MsgBox Default
Return
}
Another important case is selecting one of several values (expression-switch). Here is a workaround for that
Code:
select(i,x1,x2="",x3="",x4="",x5="",x6="",x7="",x8="",x9="") {
Return x%i%
}
You might need another function called in the first parameter, which converts other kind of data to numbers. The drawback comes from the lack of lazy parameter evaluation in AHK: all the parameters are assigned to local variables, although only one is used, which is slow.