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

Re: Surprising parsing result with anonymous functions and for loops



On 24 September 2014 16:26, Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> On Wed, 24 Sep 2014 16:07:36 +0200
> Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>> I haven't looked at the parsing for the anonymous function stuff, but
>> if it's not too hairy to fix, my vote is we drop this easter egg at
>> some point.
>
> Unfortunately I think defining functions without braces is part of the
> standard, though little used these days.  This would have to be a
> special case for anonymous functions, which is less useful.
>
> pws

Yes, I'm aware of that, and it's not a problem at all for normal functions;
% foo() for a { echo $a } ls
zsh: parse error near `ls'

Just in case something is unclear, let me try and clarify.
% () echo inside the function; echo outside the function
inside the function
outside the function

% foo () echo defining the function, $@; foo calling the function
defining the function, calling the function

The above two are perfectly okay. Now let's see when there's no
function involved,
% for a (foo bar) { echo $a }
foo
bar
% for a (foo bar) { echo $a } bing baz
zsh: correct 'bing' to 'big' [nyae]? n
zsh: parse error near `bing'

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. And writing anything after the end of a
for loop without a separator is also clearly a syntax error. Now comes
the surprising part again and hopefully this time it is surprising,
% () for a { echo $a } bing baz
zsh: correct 'bing' to 'big' [nyae]? n
bing
baz

And again, just to clarify, this is still correctly rejected:
% foo () for a { echo $a } bing baz
zsh: correct 'bing' to 'big' [nyae]? n
zsh: parse error near `bing'


-- 
Mikael Magnusson



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