[Wish] Try() Expression returning Unset
Posted: 05 Sep 2023, 08:31
Over the past years of 2.0, the general trend seems to have been to eliminate the empty-string error returns in favor of explicit Error throws. I agree with that. Explicit is usually better than implicit. That said, it seems like we do lose a little of the quick-and-dirty scripting efficiency over time, as the errors typically need to be handled.
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:
Then in a one-liner method or callback function with some calc which may fail, but I don't care about the reason, it looks like this:
In the same way you made Throw() a function that can now be used in an expression, it might be useful if Try could get a similar treatment for expressions? I propose that it would be the same as the try with no catch, but return unset? It could enable the following:
Not sure how hard that would be to implement, since you've already got the maybe monad infrastructure and operators so well fleshed out. It would definitely improve efficiency in places where full error handling isn't necessary. With the new ?? structure, it could be equally as efficient as the old empty-string returns, but much clearer and more explicit.
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.
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.