You've probably already thought of this, but I just use a global quasi-constant called
na as a null sentry for unspecified parameters. swagfag referenced my earlier posts, but I've refined things a bit since then, especially with some of the recent additions to AHK (e.g., variadic varrefs). I'll try to state the null sentry approach more cleanly here...
This sentry can be propagated at will, but the approach does have some limitations. Ideally it would point to a singleton object that would never occur as a valid value. However, since default values must be primitives, I just set the global
na := '`vna', since the vertical tab
'`v' would rarely be present in any strings I use. So in defining defaults, I must use the string literal:
Code: Select all
static Add(message := '`vna', title := '`vna', options := '`vna') {
oWin := NotifyWindow(message, title, options)
}
Then I can propagate it as needed, and can use the global
na form (for readability and ease of typing) wherever it's finally used:
Code: Select all
class NotifyWindow {
__New(message := '`vna', title := '`vna', options := '`vna') {
(message == na ? (message := '') :0)
(title == na ? (title := '') :0)
(options == na ? (options := {}) :0)
}
}
This pattern comes up so often that I use a function to set defaults (which is much simpler now with AHK having the ability to handle variadic varrefs):
Code: Select all
setdefaults(pairs*) { ;parses variadic for varref,default pairs
loop (pairs.length//2)
if %pairs[a_index*2-1]% == na
%pairs[a_index*2-1]% := pairs[a_index*2]
}
Then, the consuming function looks a little prettier. However, this only works well with simple defaults that entail no side effects or expensive computations. Unlike the prior approach, there is no short circuit. All defaults are evaluated when setdefaults is called. In this example, it's maybe not the best, unless you have a global "constant" empty object that isn't recreated on each call (assuming your function won't be modifying that empty object):
Code: Select all
na := '`vna', empty := {} ;somewhere in your global defs
class NotifyWindow {
__New(message := '`vna', title := '`vna', options := '`vna') {
setdefaults(message,'' , title,'' , options,empty)
}
}
N.B. Since there are no true constants in AHK, I've occasionally experienced self-inflicted errors related to the global na or empty value being altered accidentally.
Caveat emptor. Anyhow, hope that's maybe helpful in some way.