Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Is the function $f defined and is not an autoload stub?



On Jun 15, 11:24pm, Daniel Shahaf wrote:
}
} I'd like to test whether the shell function $f is available (callable).

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.

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:

    if [[ "$functions[$f]" != *"builtin autoload -X" ]] ||
       ( unfunction -- "$f" && autoload +X -- "$f" ); then
        print "Hooray, $f is callable"
    fi

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).



Messages sorted by: Reverse Date, Date, Thread, Author