Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] Fix ERR_EXIT behavior in function calls and "always" statements



On Wed, Nov 23, 2022, at 4:43 AM, Philippe Altherr wrote:
> I think POSIX indirectly mandates the behavior. So far, there are two 
> proposals on how to specify anonymous functions:
>
> A) a function call (to a function defined on the spot and undefined 
> right after the call)
> B) a compound command

Anonymous functions are complex commands regardless; they are parsed
much like function definitions.  The question here is only about
how they should interact with ERR_EXIT.


> In my opinion, the name and the description of anonymous functions 
> strongly suggests that anonymous functions are a shorthand for defining 
> a function, calling it with the provided arguments, and undefining the 
> function. I have always assumed that "() { <body> } <args>" is 
> syntactic sugar for "anon() { <body> }; { anon <args> } always { 
> unfunction anon }". As far as I can tell, Zsh effectively implements 
> anonymous functions with function calls. If this is indeed the case and 
> one agrees with the specification described here, then everything is 
> consistent; anonymous functions look, feel, and behave like function 
> calls, including when it comes to ERR_EXIT, and this with and without 
> my patches.

This "syntactic sugar" argument is flawed in its very conception
because the POSIX specification for "set -e" is *all about syntax*.
It addresses the early-exit behavior of various *syntactic* command
forms -- pipelines, compound commands, AND-OR lists.  It does not
know or care about function calls; it does not mention them even
once.  They are just simple commands as far as POSIX "set -e" is
concerned.

To be clear, there is nothing wrong with considering anonymous
functions to be syntactic sugar for defining, calling, and removing
a function, but that does not change their syntax.  They *are*
complex commands.  It doesn't matter how they're implemented or how
you want to think about them -- you cannot reasonably use POSIX to
justify giving them the same early-exit behavior as vanilla function
calls.

Of course, that need not stop us from doing so.  POSIX compliance
is not a requirement for zsh, anonymous functions are already well
outside the POSIX spec, and carving out a (...)-like exception for
them (as Bart originally suggested) has a lot to recommend it (as
I agreed with in my response to Peter).  But at this point POSIX
is more or less out of the picture.  Which is fine.


> You propose to specify anonymous functions as a kind of compound 
> command.

No, I did not propose that.  I presented two possibilities for
anonymous functions' behavior with respect to ERR_EXIT, elaborated
on "like other complex commands" because I thought the argument for
"like regular function calls" was self-evident, and explicitly
expressed ambivalence about which was best.

Plus, they already are complex commands.


> I fear that anonymous functions as compound commands require a more 
> complicated mental model than anonymous functions as function calls.
> For example, if anonymous functions are compound commands then I would 
> expect that the "return" in the code below exits the function "foo" but 
> that's not what it does.
>
>> foo() {
>>   echo foo-start;
>>   () { echo args: $@; return } 1 2 3
>>   echo foo-end;
>> }
>> foo

Why would you expect that?  "Complex command" is a syntactic
classification that doesn't imply any particular behavior.  It
includes {...}, (...), function definitions, the various loops and
conditionals, etc., but these all behave differently.


-- 
vq




Messages sorted by: Reverse Date, Date, Thread, Author