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

Re: wheels within wheels

30.09.2015, 07:16, "Ray Andrews" <rayandrews@xxxxxxxxxxx>:
> On 09/29/2015 08:40 PM, Bart Schaefer wrote:
>>  }
>>  } I just learned that it's possible to declare a function within another
>>  } function. A strange liberty.
>>  Interpreted language.
> Yes. It's so easy to forget that.
>>       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
>>       }
> Now that is mind expanding. If you tried that in C you'd collapse the
> universe.
> Interpreted .... sure, it can saw off the branch it's sitting on because
> there is no
> tree. Each command is past when it's past so that must mean that the
> address of
> first foo ... is there ... yes of course there is, the thing is in
> memory ... just aborts
> when second foo comes along. It will expect grammatical completion of
> first foo
> for the sake of etiquette. And when foo calls itself, it calls new foo
> which is
> not recursive. I am not yet able to snatch the pebble out of your hand,
> but that's
> a zsh koan.

Nested *same* function definition is rather useful for initialization. Or when you need to alter the implementation based on some condition, and want to cache this to not check this condition again:

    # This will parse json array passed as argument to a shell array $reply.
    # Depending on whether zpython module is available it will either use built-in python interpreter support
    # which is faster or call python in a subshell.
    parse_json_array() {
        if zmodload libzpython 2>/dev/null ; then
            parse_json_array() {
                zpython 'import zsh, json; zsh.setvalue("reply", json.loads(zsh.getvalue("1")))'
            parse_json_array() {
                reply=( "${(@0)"$(python -c 'import sys, json; sys.stdout.write("\0".join(json.loads(sys.argv[1])))' "$1")"}" )
        parse_json_array "$@"

. Also this is a feature sometimes useful for metaprogramming: you can create a function which defines a function through eval. Will be needed if you for some reason want something like bash `export -f` feature.

And this is *not* a zsh koan. I have been using the same technique for the same reasons in Python: e.g. in https://github.com/ZyX-I/powerline/blob/5ebf0875b85f85e7cd4c1e07aa8cdebbcc3bd17e/powerline/segments/common/bat.py#L163. Many interpreted languages allow this. Specifically *all* POSIX-like shells I know allow this: https://github.com/ZyX-I/powerline/blob/5ebf0875b85f85e7cd4c1e07aa8cdebbcc3bd17e/powerline/bindings/shell/powerline.sh#L74-L99 is known to work in dash, [m]ksh, busybox ash and bash.

In C you may do something similar if you use function pointer as a public interface and store different address, but this makes call indirect. If you are OK with binding yourself to GCC you can even have nested definitions.

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