bug-bash
[Top][All Lists]
Advanced

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

Re: [PATCH v2 5/8] builtins/source: parse the -i option


From: Lawrence Velázquez
Subject: Re: [PATCH v2 5/8] builtins/source: parse the -i option
Date: Fri, 24 May 2024 19:45:13 -0400
User-agent: Cyrus-JMAP/3.11.0-alpha0-480-g515a2f54a-fm-20240515.001-g515a2f54

On Fri, May 24, 2024, at 9:04 AM, Koichi Murase wrote:
> 2024年5月24日(金) 19:18 Martin D Kealey <martin@kurahaupo.gen.nz>:
>> On Tue, 21 May 2024 at 23:16, Koichi Murase <myoga.murase@gmail.com> wrote:
>>> However, I personally do not think the FPATH mechanism is useful
>>> because a file can only contain one function per file. Significantly
>>> non-trivial functions are usually implemented by a set of helper
>>> functions or sub-functions.
>>
>> Defining extra (private) functions in a file loaded from FPATH does no harm,
>> as long as its name doesn't conflict.
>
> I was assuming the Zsh's implementation of $FPATH.  I thought the Zsh
> implementation of $FPATH doesn't allow it, but I now learned that ksh's
> implementation of $FPATH is different.  I'm not a user of Zsh, so maybe I miss
> something, but Zsh's default autoload assumes that each file contains the
> *body* of the function (instead of the function declaration).  So if one puts 
> a
> set of functions in a file `func':
>
>   internal1() { ...; }; internal2() { ...; }; func() { ...; }
>
> and registers it through `autoload -U func', it would result in a big function
> containing function definitions:
>
>   func() { internal1() { ...; }; internal2() { ...; }; func() { ...; }; }
>
> This is not an expected one.

On first run "func" would define all "inner" functions -- including
replacing itself -- and subsequently behave just as if it had been
autoloaded ksh-style.  So it's possible to define helper functions
in zsh-style autoloaded functions, but it goes against the grain a bit.

        % cat /tmp/func
        internal1() { echo internal1 }
        internal2() { echo internal2 }
        func() { internal1; internal2 }
        % (autoload -Uk /tmp/func; func)
        internal1
        internal2
        % (autoload -Uz /tmp/func; func; func)
        internal1
        internal2

Moving the extra "func" call into the definition itself takes that
off the caller's hands while maintaining the ability to use "func"
as a standalone script (the motivation for the zsh style).

        % cat /tmp/func
        internal1() { echo internal1 }
        internal2() { echo internal2 }
        func() { internal1; internal2 }
        func
        % autoload -U /tmp/func
        % func
        internal1
        internal2

-- 
vq



reply via email to

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