Zsh Mailing List Archive
Messages sorted by:
Re: Is the function $f defined and is not an autoload stub?
- X-seq: zsh-users 21683
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Is the function $f defined and is not an autoload stub?
- Date: Tue, 21 Jun 2016 01:41:30 +0000
- In-reply-to: <160616091615.ZM24804__45250.email@example.com>
- List-help: <mailto:firstname.lastname@example.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:email@example.com>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <20160615232447.GA27645@tarsus.local2> <160616091615.ZM24804__45250.firstname.lastname@example.org>
Bart Schaefer wrote on Thu, Jun 16, 2016 at 09:16:14 -0700:
> On Jun 15, 11:24pm, Daniel Shahaf wrote:
> } I'd like to test whether the shell function $f is available (callable).
> Addressing the question of how to do it *now*, I think I'd choose to
> do the test in a subshell so that testing doesn't change the state
> of the calling shell. If that's acceptable, then:
No objections to a subshell. (I'd like to minimise the number of forks,
but a subshell v. a command substitution shouldn't make a difference.)
> if [[ "$functions[$f]" != *"builtin autoload -X" ]] ||
> ( unfunction -- "$f" && autoload +X -- "$f" ); then
> print "Hooray, $f is callable"
> I prefixed the "builtin ..." with a * just in case at some point in
> the future we begin including the "# undefined" comment in the body
> for $functions[$f] the same way we do for $(functions $f).
Thanks, this is saner.
> I suppose for consistency "autoload xyz" should return false if the
> function is already fully defined (though what to do in the case of
> "autoload xyz pdq" where one is defined and the other is not is a
> bit ambiguous even for the +X case). Right now "autoload xyz" never
> returns nonzero that I can tell.
So, if I read you correctly, this would enable the idiom 'if autoload +X
$f; ! autoload $f; then' to test for fully-definedness?
I _would_ like having an API for this (as opposed to testing stringified
function bodies). I don't know what it should look like, though; I just
know that I have a need for it.
The proposed would cause behaviour changes in certain cases: for
example, in the case of a plugin that calls 'autoload add-zsh-hook &&
add-zsh-hook foo bar' with errexit or printexitvalue in effect. (Yes,
people do this: the idiom I learnt was "autoload foo && foo".)
[I'm not saying this is or isn't a problem; I'm simply stating a fact
about the proposed change.]
Messages sorted by: