zsh-users
 help / color / mirror / code / Atom feed
From: Kurtis Rader <krader@skepticism.us>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: wheels within wheels
Date: Tue, 29 Sep 2015 19:55:36 -0700	[thread overview]
Message-ID: <CABx2=D_2aXYy1GaZ-d719ZvDQxGD2NwT+KxG7NTwjhGpjkV2OA@mail.gmail.com> (raw)
In-Reply-To: <560B1BE7.8020507@eastlink.ca>

[-- Attachment #1: Type: text/plain, Size: 1599 bytes --]

On Tue, Sep 29, 2015 at 4:16 PM, Ray Andrews <rayandrews@eastlink.ca> wrote:

> I just learned that it's possible to declare a function within another
> function.  A strange liberty.  Why would one want to, and what are the
> gotchas of doing so?
>

Many compiled languages support the concept of "closures". Even those, like
Python, which are compiled on the fly into byte-code as they are executed.
On the other hand zsh, and in general any shell, based on bash or csh do
not support closures or namespaces so I have no idea why someone would nest
function definitions in those shell languages. Google "zsh closures" and
read this answer:

http://unix.stackexchange.com/questions/94129/is-there-something-like-closures-for-zsh


In short: don't do it. 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. Anonymous functions defined within another function are a different
matter but outside the scope of this question.

I've seen some pretty amazing bash/csh/ksh/zsh scripts in my life.
Including one that implemented an assembly language compiler. But if you
need that level of complexity you should be using a different language. If
the only tool you know how to use is a hammer then every problem looks like
a nail.

#!/bin/zsh

function foo {
    function bar {
        echo hello from bar
    }

    echo hello from foo
    bar
}

echo calling foo
foo
echo calling bar
bar

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

  reply	other threads:[~2015-09-30  2:55 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17 15:54 autoload Ray Andrews
2015-09-17 17:34 ` autoload Bart Schaefer
2015-09-17 19:22   ` autoload Ray Andrews
2015-09-17 20:40     ` autoload Bart Schaefer
2015-09-17 23:06       ` autoload Ray Andrews
2015-09-17 23:20         ` autoload Bart Schaefer
2015-09-18  1:20           ` autoload Ray Andrews
2015-09-18  4:04             ` autoload Bart Schaefer
2015-09-18  5:00               ` autoload Ray Andrews
2015-09-18  5:52                 ` autoload Bart Schaefer
2015-09-18 15:49                   ` autoload Ray Andrews
2015-09-18 16:52                     ` autoload Bart Schaefer
2015-09-18 18:29                       ` autoload Ray Andrews
2015-09-18 19:02                         ` autoload Bart Schaefer
2015-09-18 22:57                           ` autoload Ray Andrews
2015-09-19  0:14   ` autoload Bart Schaefer
2015-09-19 15:04     ` autoload Ray Andrews
2015-09-19 16:29       ` autoload Bart Schaefer
2015-09-19 18:13         ` autoload Ray Andrews
2015-09-19 21:22           ` autoload Bart Schaefer
2015-09-19 22:12             ` autoload Ray Andrews
2015-09-20  5:53               ` autoload Bart Schaefer
2015-09-20 15:37                 ` autoload Ray Andrews
2015-09-20 15:59                   ` autoload Bart Schaefer
2015-09-20  0:58             ` autoload Ray Andrews
2015-09-20  5:41               ` autoload Bart Schaefer
2015-09-20 23:21                 ` autoload Ray Andrews
2015-09-21  4:18                   ` autoload Bart Schaefer
2015-09-21 17:03                     ` autoload Ray Andrews
2015-09-21 18:17                       ` autoload Bart Schaefer
2015-09-21 20:09                         ` autoload Ray Andrews
2015-09-22  3:19                           ` autoload Bart Schaefer
2015-09-22 17:33                             ` autoload Ray Andrews
2015-09-23  4:39                               ` autoload Bart Schaefer
2015-09-23 15:06                                 ` autoload Ray Andrews
2015-09-29 23:16                             ` wheels within wheels Ray Andrews
2015-09-30  2:55                               ` Kurtis Rader [this message]
2015-09-30  3:24                                 ` Ray Andrews
2015-09-30  3:40                                 ` Bart Schaefer
2015-09-30  4:03                                   ` Mikael Magnusson
2015-09-30  4:15                                   ` Ray Andrews
2015-09-30  7:05                                     ` Bart Schaefer
2015-09-30 15:06                                       ` Ray Andrews
2015-09-30  8:01                                     ` ZyX
2015-09-30 15:18                                       ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABx2=D_2aXYy1GaZ-d719ZvDQxGD2NwT+KxG7NTwjhGpjkV2OA@mail.gmail.com' \
    --to=krader@skepticism.us \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).