1

I was running into a flycheck warning about a function which may not be defined.

Since the function is a small optional dependency, I am inlining it in case the package that provides cannot be loaded.

I first tried I checking if the package failed to be required before filling in the function:

(unless (require 'selcand nil t)
  (defun selcand-select () ...))

This gave me

the function ‘selcand-select’ is not known to be defined.

Which makes sense since the compiler has no idea whether the selcand package will define the function selcand-select.

So I added an explicit fboundp check:

(unless (and (require 'selcand nil t) (fboundp 'selcand-select))
  (defun selcand-select (cands &optional prompt stringify) ...))

which still gave me the same error.

1

What did silence the warning was making the condition simpler for the compiler:

(require 'selcand nil t) 
(unless (fboundp 'selcand-select)
    (defun selcand-select (cands &optional prompt stringify) ...))

Interestingly the following also works:

(require 'selcand nil t) 
(unless (and (require 'selcand nil t) (fboundp 'selcand-select))
    (defun selcand-select (cands &optional prompt stringify) ...))

Earlier I had also tried wrapping the form in 'eval-when-compile:

(eval-when-compile
  (unless (require 'selcand nil t)
    (defun selcand-select () ...)))

which failed with:

the function ‘selcand-select’ might not be defined at runtime.

This also makes sense for the same reason that my first approach didn't work, and eval-when-compile is not needed here since selcand-select is not a macro.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.