Create a $&throwonfalse primitive and false exception handling#280
Open
jpco wants to merge 2 commits into
Open
Create a $&throwonfalse primitive and false exception handling#280jpco wants to merge 2 commits into
$&throwonfalse primitive and false exception handling#280jpco wants to merge 2 commits into
Conversation
This primitive is intended to replace $&exitonfalse, but its behavior isn't quite sorted out yet. Because of this, we're leaving it undocumented and unused by default for now. Using exceptions to implement the -e flag is the Right Thing.
$&throwonfalse primitive and false exception handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This primitive is intended to supplant
$&exitonfalse. When using$&throwonfalse, the shell will raise afalseexception if a command produces a falsey result. The argument to thefalseexception is the value which triggered it.falseis caught by$&if(for commands which are run as tests) and by the<=construct.For example,
echo <={result first; result second}with$&throwonfalsewill printfirst, as theresult firstwill cause afalse firstexception to be raised, which will then be caught by the<={}and used as if it were a normal result.Using exceptions to implement the
-eflag is really the Right Thing:Running cleanup code after a false result in an
es -escript is impossible today, which is a really bad limitation of the-eflag. Other shells have something like rc'ssigexit, or bash'strap EXIT, to perform cleanup in this case, but es doesn't use traps for dealing with interruptions: it uses exceptions.Exceptions allow more flexible handling than the signal/exit/error traps of other shells. For example, using
es -einteractively is totally reasonable with an%interactive-loopwhich catches and handlesfalse. Moreover, the behavior is flexible: you could have it quietly treat the exception like any other result (the default), you could have it catch and print any false results, or you could have it make the interactive shell abruptly exit, if for some strange reason you wanted a more "traditional" experience.Using exceptions allows us to avoid the traditional warts of the
-eflag. In other shells (and in es today) commands withiniforwhiletests are run with the-ebehavior dynamically disabled. This causes enough inconsistency and unpredictability in what is supposed to be a "safety" mechanism that many people recommend avoiding using-eat all. However, with exceptions, and with anifand<=which catch the exception to use the value, we avoid any need to conditionally disable the behavior. (On top of this, es' more complete return values from pipes mean that thepipefailoption isn't necessary or useful at all.)I am proposing this behind a
#defineas an alternative to$&exitonfalsefor now because I have already iterated a fair bit on the design of thefalseexception up to this point, and I want to make sure it really gets hammered out before it becomes the Official, Documented, and Only Behavior.