zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Marlon Richert <marlon.richert@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: How to “namespace” an autoloaded function?
Date: Mon, 25 Oct 2021 09:49:37 -0700	[thread overview]
Message-ID: <CAH+w=7aN50ypC3h0j1nTa7ieBmsO8cYm0zMXYnCfyih0xfcKRw@mail.gmail.com> (raw)
In-Reply-To: <CAHLkEDs-Tq7VBe7O_dHqqWEZmM7oBJFiQ6BO8Wxi9ySaV5R_wA@mail.gmail.com>

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.


      reply	other threads:[~2021-10-25 16:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-24  9:17 Marlon Richert
2021-10-24 23:44 ` Bart Schaefer
2021-10-25  7:36   ` Marlon Richert
2021-10-25 16:49     ` Bart Schaefer [this message]

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='CAH+w=7aN50ypC3h0j1nTa7ieBmsO8cYm0zMXYnCfyih0xfcKRw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=marlon.richert@gmail.com \
    --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).