- [EDIT:] And further functions here ranked by usefulness:
https://autohotkey.com/boards/viewtopic.php?f=13&t=64405&p=277017#p277017
- If built-in, you would probably use them, otherwise, you would use more verbose or cryptic code.
- They add clarity and elegance to scripts, and are more instructive, regarding intention, than the alternatives. Cf. the advantages brought by the Min/Max functions.
- (I view potential StrCount/StrRept functions as having similar advantages.)
- Swap: avoid using a temporary variable when swapping variables, simpler.
- Avoid time spent double-checking the code.
- (Perhaps also: ObjSwap (or ObjSwapKeys).)
- Assign (lower priority): saves writing out long variable names twice.
- Assign could also be used to write directly to A_ variables, making various SetXXX commands redundant. (Or are those A_ variables going to become writable in AHK v1 at some point?)
- Sign: avoid cryptic (n > 0) - (n < 0) or using a ternary operator.
- Simplifies longer expressions that check the sign.
- (An alternative to Sign(Num) would be NumCompare(Num, 0).)
- Some example code:
Code: Select all
q:: ;swap values
var1 := "a", var2 := "b"
Swap(var1, var2)
MsgBox, % var1 " " var2
oArray := ["a", "b"]
ObjSwap(oArray, 1, 2)
MsgBox, % oArray.1 " " oArray.2
return
Swap(ByRef var1, ByRef var2)
{
temp := var1
var1 := var2
var2 := temp
}
ObjSwap(obj, key1, key2)
{
temp := obj[key1]
obj[key1] := obj[key2]
obj[key2] := temp
}
;==================================================
w:: ;assign
var := "old value"
MsgBox, % var
old := Assign(var, "new value")
MsgBox, % var " " old
return
Assign(ByRef var, value)
{
temp := var
var := value
return temp
}
;==================================================
e:: ;sign
MsgBox, % Sign(-123) " " Sign(0) " " Sign(123)
return
Sign(num)
{
return (num > 0) ? 1 : (num < 0) ? -1 : 0
;return (num > 0) - (num < 0)
}
;==================================================