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

Re: PATCH: anonymous functions (no documentation yet)



Bart Schaefer wrote:
> I like this idea although I prefer the "function { ... }" syntax to the
> empty parens with nothing before them.

It's pretty much inevitable they'll both be available.

> With FUNCTION_ARGZERO set, what's the value of $0 inside an anonymous
> function?  What's the value of $functions[$0] ?

$0 will be "(anon)" which is similar to "(eval)" in debug prompts.
There's no entry in the function table, so $functions[$0] won't be set
unless someone as defined a real function called "(anon)" which is
eccentric but valid (underlying problem is as below: *any* string
whatsoever is a valid function name; in principle you can even have
embedded nulls because this is all done internally).  $funcstack[1] will
be "(argv)".

> On Jun 26,  4:06pm, Peter Stephenson wrote:
> } Subject: Re: PATCH: anonymous functions (no documentation yet)
> }
> } Oliver Kiddle wrote:
> } >   f ''() { ... }
> } > 
> } > might have the effect of defining a function f and running it
> } > immediately. And then you might write: f '' ''() { ... } and so on.
> } 
> } That ought to be a relatively straightforward addition, I think, subject
> } to the query above.
> 
> What would the form with multiple empty-string arguments mean?  Call
> the function multiple times?  (Anyway I take it from later remarks
> that ''() already defines a function with the empty name, so probably
> this "name it but also call it" idea won't work.)

The last sentence appears to be correct.

> } > What effect does all this have on the positional parameters?
> } 
> } Positional parameters from the surrounding area would be hidden and the
> } local parameter list empty.  I could easily copy them in so that they
> } could be used and modified without affecting the calling environment.
> 
> 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.

Indeed, though unless your parameter list is humongous it won't be a big
effect.  On the other hand. humongous parameter lists are not unknown
with things like zargs.

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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