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 21,  1:41am, Daniel Shahaf wrote:
}
} > 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 was thinking more of "if ! autoload $f || autoload +X $f; then" but I
guess your way also works.  I might go so far as

  if (( $+functions[$f] )) && { ! autoload $f || autoload +X $f }; then

to avoid autoloading something that was never intended to be so, but it
depends on whether you already know $f is in $fpath or whatever.

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

Hmm.  Yes, that would be a possible issue in the case where something
else had already both autoloaded and executed $foo.

I just realized there's another way to distinguish autoloads from full
functions in current versions:

  if (( $+functions[$f] )); then
    if [[ -z ${(M)$(typeset +f -u):#$f} ]]; then
      print "$f is already fully defined"
    elif ( autoload +X $f ); then
      print "$f is callable"
    fi
  fi

Remove the subshell in the elif when you don't care whether you force
the function to load in the current shell.



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