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

Re: Surprising parsing result with anonymous functions and for loops



On Wed, Sep 24, 2014 at 3:02 PM, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>
> Okay, so an anonymous function without braces should not possibly be
> able to take arguments, because they are either part of the single
> command in the function, or if you have a ;, they are not part of the
> function definition at all.

That's not quite how it works.  The foo() case is a function
definition, but the "anonymous" case is simultaneously a definition
and a call.  In the latter situation, the parser consumes a valid
function definition, and then anything left over is its arguments.
Since "for a { echo $a }" is a valid function body, the parse switches
to looking for arguments after that point.  In the former case, there
is no implicit function call, so it is an error for anything to be
left over.

The presence of braces around the entire body simply allows the parser
to distinguish the body from the arguments.  If there are other
syntactic clues, those also distinguish body from arguments, e.g., the
token "done" here:

() for a; do echo $a; done bing bong

Note that the semicolons didn't terminate the function definition,
because they are part of the for-do-done compound syntax.



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