From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5358 invoked by alias); 30 Sep 2015 03:40:54 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20662 Received: (qmail 1844 invoked from network); 30 Sep 2015 03:40:53 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:in-reply-to:comments:to:subject:mime-version :content-type; bh=G8R0JqU1xe0ZTHkVrBcWBIuilF/5AO1MHpXcjpEY6Tc=; b=LtgrxcJ8RKhBSPbg203q3OwKUEsWvZNLcRm2vXwDjIMnN8/M3+zjN8bIJV5z/1W2WH 6WQJTiHYipMF464eXFyusw1k8q4yL/LxnSIzv0jQzJ8WDZjwlSovcBgeQxhgEiRD4ooF 923EyYRGDKRQPkhRPtWvs2GmDJms4oFicHoYOidja+EDX7g/8A9iAZn5alELAqIMEZ6n stWlbt5zxJaZELF42j/lzisxuDxN5oNFf8U1tamI2fu1DSdN1Z0dhNznjzZ0FY7R0aNo ++wh1OZao0IX0kfDCNPU7tnsjvcp11D+8Fg92EOrJnXHQEUe6nLb/HtcHXAejZQu2LrU ZxDg== X-Gm-Message-State: ALoCoQn2uFlUMRFk0/9wFY9xmyXIH9ONpwZlqeBx6p3gxpon7sczc059oN8+I8JzlTLbPb3AgK5K X-Received: by 10.60.41.9 with SMTP id b9mr864700oel.37.1443584450569; Tue, 29 Sep 2015 20:40:50 -0700 (PDT) From: Bart Schaefer Message-Id: <150929204047.ZM9646@torch.brasslantern.com> Date: Tue, 29 Sep 2015 20:40:47 -0700 In-Reply-To: <560B1BE7.8020507@eastlink.ca> Comments: In reply to Ray Andrews "wheels within wheels" (Sep 29, 4:16pm) References: <55FAE223.2080502@eastlink.ca> <150917103419.ZM10067@torch.brasslantern.com> <150918171441.ZM27212@torch.brasslantern.com> <55FD7982.9030505@eastlink.ca> <150919092922.ZM28214@torch.brasslantern.com> <55FDA5D3.9020304@eastlink.ca> <150919142243.ZM23634@torch.brasslantern.com> <55FE04AD.1070304@eastlink.ca> <150919224120.ZM4736@torch.brasslantern.com> <55FF3F7E.4060906@eastlink.ca> <150920211840.ZM31871@torch.brasslantern.com> <5600386E.7060201@eastlink.ca> <150921111746.ZM388@torch.brasslantern.com> <56006401.5060902@eastlink.ca> <150921201943.ZM707@torch.brasslantern.com> <560B1BE7.8020507@eastlink.ca> In-Reply-To: Comments: In reply to Kurtis Rader "Re: wheels within wheels" (Sep 29, 7:55pm) X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: wheels within wheels MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Sep 29, 4:16pm, Ray Andrews wrote: } Subject: wheels within wheels } } I just learned that it's possible to declare a function within another } function. A strange liberty. Interpreted language. Defining a function is just like executing any other command, so you can do it anywhere a command is allowed. There really isn't any such thing as "declaring" a function -- either it is defined [the command that defines it has been *executed*, not merely written out as part of some dormant code] or it isn't. The "functions -u" aka "autoload" command is the closest there is to "declaring" a function, but really it defines a function with an empty body which is to be filled in later. This is inaccurately referred to as an "undefined" function because that's shorter to say. } Why would one want to, and what are the gotchas of doing so? Consider it a sort of conditional compilation, if you need a comparison. Like the _git and tetris examples in the autoload thread -- various helper functions get defined only if/when the main function that needs them is actually called. In addition to what Kurtis says, a gotcha with nesting functions is that every time the outer function is called, it re-defines the inner functions (deletes the old definitions and re-create them). Remember, interpreted language. Everything interesting happens at run time. Therefore do not nest function definitions unless you understand very well what they are doing. } BTW Bart, any further word on where to find the latest manual? You should use the manual for the version of zsh that you have. If you start looking at the latest manual but you're not using the latest shell, you may get just as much misinformation as if you're looking at an older manual. } hot of the press. Is this a 'git' sort of thing? Yes. On Sep 29, 7:55pm, Kurtis Rader wrote: } } In short: don't do it. That's a bit too short. Nesting function definitions is exactly what a lot of autoloadable functions do. The difference is that autoloadable functions also redefine *themselves* this way, to change what happens the second time the "same" function (in name only) is called. } You can see for yourself that nested, named, } function definitions are in fact globally visible (as opposed to visible } only to the function in which they're defined) by executing the following } script. Here's a modification of that script: function foo { echo defining bar function bar { echo hello from bar } echo redefining foo function foo { echo hello from new foo bar } # this looks like infinite recursion, # but it is not, because foo was redefined foo } # This will say "command not found" echo calling bar before foo bar echo calling foo foo echo calling bar after foo bar echo calling foo again foo