Code: Select all
sum(a, b?, c?) {
b := IsSet(b) ? b : 0
c := IsSet(c) ? c : 0
return a + b + c
}
Code: Select all
sum(a, b?, c?) {
b := IsSet(b) ? b : 0
c := IsSet(c) ? c : 0
return a + b + c
}
Code: Select all
fn(a, b := unset, c*) {
; Do something
anotherfn(a, b?, c[1]?)
}
anotherfn(a, b:=2, c:=3) => a + b + c
Code: Select all
Function(p := unset)
{
if (IsSet(p))
return p
else
return "unset"
}
Code: Select all
MsgBox IsSet(p) ? p : "unset"
Code: Select all
MsgBox IsSet(p) ?? "unset"
Code: Select all
sum(a, b?, c?) => a + (b ?? 0) + (c ?? 0)
Code: Select all
#Requires AutoHotkey v2.1-
MsgBox fn(1, 2, 3, 4) ; Extra Parameter
MsgBox fn(1, 2, 3)
MsgBox fn(1, 2) ; Missing Parameter
fn(p*) => forward(p[1], p[2]?, p[3]?) ; Destructure Array
forward(a, b:=0, c:=0) => a + b + c
Code: Select all
#Requires AutoHotkey v2.1-
class x {
static y => this.z ; UnsetError!
}
MsgBox(x.y?) ; Not shown.
A function doesn't know whether its return value is being used or how, so can't be the one to throw UnsetError. UnsetError is thrown by the expression evaluator only if unset is not permitted. A statement such as fn() or expression like (fn(), true) also permits unset, because any value would only be discarded.By default, any attempt to use an unset variable, property, array element or return value immediately causes an UnsetError to be thrown.
It does not "catch UnsetError under specific conditions" or the like. If an error is thrown, it is propogated regardless of ? or the type of error. If ? worked by catching UnsetError, it would be very difficult to distinguish between an UnsetError intended for the operator and an UnsetError thrown by a recursive call, which could come from a different call on the same line. It would also be very slow.Permits a variable (or in [v2.1-alpha.2+], a property, method or function call) to be unset.