zsh-users
 help / color / mirror / code / Atom feed
* Backward-compatible anonymous functions?
@ 2011-06-07 15:39 Benjamin R. Haskell
  2011-06-07 17:07 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Benjamin R. Haskell @ 2011-06-07 15:39 UTC (permalink / raw)
  To: Zsh Users

I have a system I don't administer where I'd like to use the same .zsh 
files I use on all my other systems.  It's running zsh-4.3.6, so it 
doesn't have "anonymous functions" (I'll use "anonymous scopes" 
hereafter).

For the (A) glob qualifier, introduced sometime in 4.3.9, I did this:

autoload -z is-at-least
if is-at-least 4.3.9 && (( ! $+in_solaris )) ; then
 	A () { reply=( $REPLY(:A) ) }
else
 	A () { reply=("$(perl -MCwd=realpath -we 'print realpath shift' $REPLY)") }
fi

Then, I can use (+A) everywhere instead of (A), and it works, at least 
from from 4.3.2 up (That's the lowest Zsh version I use on a regular 
basis -- not by choice), presumably with `perl`s greater than somewhere 
in the 5.5 range.

When I figured out that the anonymous scope was what caused the problem 
here:

() {
 	# this stuff happily parsed, but not executed
}

I found a nice, simple detection mechanism (better than relying on 
is-at-least):

() { false } && has_anon=false || has_anon=true

But, I couldn't think of a similarly-simple transformation (akin to 
"change (A) to (+A)") that I could apply where I used anonymous scopes. 
Anyone here have a good one?

-- 
Best,
Ben


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Backward-compatible anonymous functions?
  2011-06-07 15:39 Backward-compatible anonymous functions? Benjamin R. Haskell
@ 2011-06-07 17:07 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2011-06-07 17:07 UTC (permalink / raw)
  To: Zsh Users

On Jun 7, 11:39am, Benjamin R. Haskell wrote:
}
} () {
}  	# this stuff happily parsed, but not executed
} }
} 
} But, I couldn't think of a similarly-simple transformation (akin to 
} "change (A) to (+A)") that I could apply where I used anonymous scopes. 
} Anyone here have a good one?

I used to use "repeat 1 { ... }" for this kind of thing but of course
that doesn't have local options/variables.

The trouble is that to implement a scope you have to first define it and
then call it.  That requires a syntactic transformation that's probably
impossible in zsh because functions won't accept a block as an argument,
and aliases can only expand in place, they can't grab the command line
and append something at the end.

You might be able to write a scope push/pop module along the lines of
what I played with in workers/28271, but the popping part may still be
a problem.  I suppose if you created a builtin "toggle_scope" that
returns true when pushing a new local scope and false when popping to
the previous scope, then you could write

while toggle_scope; { ... }

and then the loop would execute once and the loop body would be in a
new scope.  Needs further thought to handle nested scopes, etc.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-06-07 17:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-07 15:39 Backward-compatible anonymous functions? Benjamin R. Haskell
2011-06-07 17:07 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).