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

Re: PATCH: anonymous functions, full patch



On Jun 30, 11:27am, Peter Stephenson wrote:
}
} > > I'm of two minds on that score.  On the one hand it'd be nice to be able
} > > to get at the surrounding $@.  On the other it may often be a needless
} > > expense.
} 
} The way the internals work we would have to copy the list to pass down, but
} we wouldn't have to copy the strings.  Anything cleverer (that makes a
} significant difference) is quite a lot of work.  I've left this out for
} now.

Seems a little odd, though, to call something a function when you can't
pass any arguments to it.  I'm not going to have time to look today, but
how difficult is this?

    function { print $@ } args go here

Currently that's a parse error, so would not conflict with any existing
scripts.  [Of course (){ print oops } is NOT a parse error, which means
in some theoretical world somebody could be using it {un}intentionally
as a no-op block and we're about to mess them up.  Not a great worry.]

There's another syntax that this anonymous functions patch creates:

torch% function() { echo $0 }  
(anon)

Since only empty parens are currently valid there [anything else is a
parse error] it'd also be possible to use

    function(args go here) { print $@ }

but in other languages that would be naming the positional parameters
of a closure, not passing it arguments, so I suspect we should reserve
this syntax for that future enhancement.

Put a space between "function" and "()" and you have a fourth syntax,
where non-empty parens are parsed as a glob qualifier on an empty file
name and therefore either produce an error or always fail to match.  I
don't see anything useful there.

Two final remarks:  One: should this be considered a bug?

torch% torch% () ()
function function> {                 
function function> print oops
function function> }
zsh: parse error near `\n'

Once you've got the double empty parens there seems to be nowhere to go
that doesn't end in a parse error.  [If the second set of parens is not
empty you get a function whose body is a subshell.]

Two:  As usual with function definitions, the body doesn't have to be a
block.

torch% () print $0
(anon)
torch% x=3
torch% () local x=4
torch% print $x
3



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