For example, I do have occasion to want to write a quick one-liner function, whose failure for any reason should return as some default value. Usually that's an empty string. Sometimes it's 0 or a null sentinel. With some of these recent changes to allow unset returns, I'm thinking I will modify those null sentinel instances to return unset (if I can figure it out), since it works so nicely with the maybe operators. Currently anyhow, fwiw, my trivial implementation looks like this:
Code: Select all
tryd(fnc, dft) { ;try or default val
try return fnc()
return dft
}
trye(fnc) => tryd(fnc, '') ;try or empty string
tryz(fnc) => tryd(fnc, 0) ;try or zero
Code: Select all
calc(other) => tryz(()=>(this/other))
Code: Select all
calc(other) => try(this/other) ;if you want an unset return on failure
calcz(other) => try(this/other) ?? 0 ;or if you want a 0 return
And thanks so much, @lexikos, for all the great work so far on 2.1. Just when I thought AHKv2 had reached nirvana, and I wouldn't need to keep changing my codebase, your 2.1 is so compelling, that I know I my codebase must have it! I write in a lot of languages, and though my dev friends berate me for it, AHK is still my favorite.