[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Closure concept in bash

From: Dan Douglas
Subject: Re: [Help-bash] Closure concept in bash
Date: Tue, 31 Jan 2012 16:59:27 -0600
User-agent: KMail/4.7.4 (Linux/3.1.6-pf; KDE/4.7.4; x86_64; ; )

> On 1/31/12 4:52 PM, Dan Douglas wrote:
> > Here's a Korn shell "closure" of sorts. Notice illustrating the
> > difference from Bash requires two different kinds of function
> > definitions:
> > 
> > $ ( f() { typeset x=5; function g { echo "$x"; }; }; f; g; )
> > 5
> > 
> > And this alone still doesn't allow you to do very much which you can't
> > already do in Bash due to the way scoping works - using the context at
> > the time the function is called.
> It works the same in bash if you remove the typeset.  The only reason it
> works this way in ksh is that typeset doesn't create local variables in
> `posix'-style functions; leaving it in simply disguises the creation of a
> global variable.  (Add `echo $x' after the call to `g' to see.)
> Chet

Oh wow, thanks, that explains a lot of quirks (I tested everything but this), 
and why there doesn't appear to be an explicit "global" or "local" flag to 

So they don't even have a way to get the kind of "cascading inheritence-like" 
scope you get from bash? I find function wrappers with callees that see locals 
of a caller and then convieniently destroy themselves when returning to really 
clean up the solutions to some otherwise hard problems, which wouldn't be 
worth the trade-offs in another langauge.

I know some consider KSH scoping to have advantages, though since the above 
doesn't work as I expected it's hard to imagine why except to avoid unexpected 
namespace collisions.
Dan Douglas

reply via email to

[Prev in Thread] Current Thread [Next in Thread]