On Tue, May 21, 2024 at 1:17 PM Koichi Murase <myoga.mur...@gmail.com>
wrote:


> There are already shell-function implementations at
> /examples/functions/autoload* in the Bash source. They reference FPATH
> to load functions, though one needs to call `autoload' for each
> function in advance (by e.g. `autoload "$fpath_element"/*' ).
>

> 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. Also, in libraries, we usually have a set
> of functions that are closely related to one another and share the
> implementations. I don't think it is practical to split those
> functions into dozens or hundreds of files. It would also be slow to
> read many different files, which requires access to random positions
> on the disk.
>

Jeez, I forgot about this examples/functions/autoload.

First of all thanx for pointing it, it demonstrate that things can be
implemented as bash code (not C hack in bash) at least in the experimental
phase.

Second, there is no such limitation about 1 source 1 function mapping,
since it is sourced any functions defines in the files become available.
This open two path.

1 package setup function is autoloaded bringing all the other function that
are now callable

Say file foo defines foo() bar() then autoload foo() brings both foo() and
bar(), if foo is the setup thing for file foo then the doc sez autoload foo
to bring up bar() along.

Another path is to have a symlink on each 'exported' function on the file
foo, so a ln -s foo bar would make bar autoloadable. Note I used 'exported'
here meaning the foo file may define a lot of function, only the exposed
API one need a symlink.

The real divergence from this autoload, and the ksh93 one is that ksh93
will try an autoload on foo using FPATH on the 'command-not-found'
situation, something bash don't handle in the user context (it does it in a
subshell for unknown reasons).
So basically in ksh93 script, beside setting FPATH, you simply call
functions directly, on command-not-found then try FPATH to find your
function and if found proceed with the load, define func, run setup code,
and run the func with parameters...

This is neat, but the fact we don't have that on bash is no big deal, we
can live without it, again no need to break things that works.

Reply via email to