* How to “namespace” an autoloaded function?
@ 2021-10-24 9:17 Marlon Richert
2021-10-24 23:44 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Marlon Richert @ 2021-10-24 9:17 UTC (permalink / raw)
To: zsh-users
What would be the canonical way to make the body of function 'foo.bar' be autoloaded from '/some/path/to/bar'?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to “namespace” an autoloaded function?
2021-10-24 9:17 How to “namespace” an autoloaded function? Marlon Richert
@ 2021-10-24 23:44 ` Bart Schaefer
2021-10-25 7:36 ` Marlon Richert
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2021-10-24 23:44 UTC (permalink / raw)
To: Marlon Richert; +Cc: Zsh Users
On Sun, Oct 24, 2021 at 2:17 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> What would be the canonical way to make the body of function 'foo.bar' be autoloaded from '/some/path/to/bar'?
There isn't a "canonical" way, really. The autoload mechanism is
entirely dependent on the file name and the function name being the
same. I can suggest a way to accomplish it, but there is probably
more than one way.
The most flexible way I can think of is to take advantage of the
"suffix alias" mechanism.
function namespacedfunction {
if [[ $1 = *.* ]] && {
[[ -n $functions[$1:e] ]] ||
autoload +X -R /some/path/to/$1:e
}
then
functions -c $1:e $1
"$@"
else
return 1
fi
}
Now
alias -s bar='namespacedfunction'
will cause "foo.bar" to invoke "namedspacedfunction" for any "foo",
link it to "bar", and then run it.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to “namespace” an autoloaded function?
2021-10-24 23:44 ` Bart Schaefer
@ 2021-10-25 7:36 ` Marlon Richert
2021-10-25 16:49 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Marlon Richert @ 2021-10-25 7:36 UTC (permalink / raw)
To: Bart Schaefer, Zsh Users
On 10/25/21, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Sun, Oct 24, 2021 at 2:17 AM Marlon Richert <marlon.richert@gmail.com>
> wrote:
>>
>> What would be the canonical way to make the body of function 'foo.bar' be
>> autoloaded from '/some/path/to/bar'?
>
> There isn't a "canonical" way, really. The autoload mechanism is
> entirely dependent on the file name and the function name being the
> same. I can suggest a way to accomplish it, but there is probably
> more than one way.
>
> The most flexible way I can think of is to take advantage of the
> "suffix alias" mechanism.
>
> function namespacedfunction {
> if [[ $1 = *.* ]] && {
> [[ -n $functions[$1:e] ]] ||
> autoload +X -R /some/path/to/$1:e
> }
> then
> functions -c $1:e $1
> "$@"
> else
> return 1
> fi
> }
Can you think of another way to do this than to autoload the function
and copy it to a new function? Given a function file 'bar', which I
want to load as 'foo.bar', there might already be a loaded function
'bar', which I don’t want to overwrite. The point of adding a
namespace to the function is to not overwrite an otherwise same-named
function, if any, which this doesn’t accomplish.
Also, what would be the best way to do this in batch form? That is, I
want to autoload _all_ function files from a particular dir as
foo.<filename> instead of just <filename>.
> Now
>
> alias -s bar='namespacedfunction'
>
> will cause "foo.bar" to invoke "namedspacedfunction" for any "foo",
> link it to "bar", and then run it.
Would this work when calling foo.bar from inside a function that was
loaded with `autoload -U`?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to “namespace” an autoloaded function?
2021-10-25 7:36 ` Marlon Richert
@ 2021-10-25 16:49 ` Bart Schaefer
0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2021-10-25 16:49 UTC (permalink / raw)
To: Marlon Richert; +Cc: Zsh Users
On Mon, Oct 25, 2021 at 12:36 AM Marlon Richert
<marlon.richert@gmail.com> wrote:
>
> On 10/25/21, Bart Schaefer <schaefer@brasslantern.com> wrote:
> > On Sun, Oct 24, 2021 at 2:17 AM Marlon Richert <marlon.richert@gmail.com>
> > wrote:
> >>
> >> What would be the canonical way to make the body of function 'foo.bar' be
> >> autoloaded from '/some/path/to/bar'?
> >
> > There isn't a "canonical" way, really. The autoload mechanism is
> > entirely dependent on the file name and the function name being the
> > same.
>
> Can you think of another way to do this than to autoload the function
> and copy it to a new function?
No. There is no mechanism for changing the name of a function while
autoloading it.
There is a way you could automatically initialize such a namespace,
but it's not exactly the same as loading it on demand in the way your
use of the term "autoload" implies. Also, if a function bar calls
another function baz, invoking the body of bar as foo.bar won't run
foo.baz under any circumstances I can imagine, so this whole idea of
namespaces falls apart pretty quickly if there is any interdependency
(or recursion that doesn't reference $0).
> Also, what would be the best way to do this in batch form? That is, I
> want to autoload _all_ function files from a particular dir as
> foo.<filename> instead of just <filename>.
Initializing the namespace goes something like this:
source <(
unfunction -m \*
fpath=(/some/path/to)
autoload +X $^fpath/*(N)
for f in ${(k)functions}
do
functions -c $f foo.$f
unfunction $f
done
functions
)
To get something more like on-demand loading, replace the "functions"
at the end with
zcompile -c $TMPPREFIX.foo
print "fpath+=($TMPPREFIX.foo.zwc);
autoload ${(k)functions}"
but that mostly saves memory rather than time because you still have
to pre-load all the function sources. (Note I did not actually test
this, so it may need adjustment.)
If your namespaces are known in advance you can create the zcompile
files in a more permanent location, and then you only have to build
them once (or each time /some/path/to changes contents).
> > alias -s bar='namespacedfunction'
>
> Would this work when calling foo.bar from inside a function that was
> loaded with `autoload -U`?
No, it would not.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-10-25 16:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-24 9:17 How to “namespace” an autoloaded function? Marlon Richert
2021-10-24 23:44 ` Bart Schaefer
2021-10-25 7:36 ` Marlon Richert
2021-10-25 16:49 ` Bart Schaefer
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).