When you perform a function call (myfunction ...
) in emacs lisp, the byte compiler will warn if the function isn't known to be defined at compile time. This helps greatly with a top-down programming style, since then I can happily write
(defun foo (bar baz)
(foo-filter-by-functon (lambda (a b) (equal (upcase a) (upcase b)))
(foo-combine-values bar baz)))
When I compile the byte compiler tells me that I haven't yet defined or misspelled the two foo-
functions.
However, if the lambda expression becomes complex some more restructuring makes sense, e.g.
(defun foo (bar baz)
(foo-filter-by-functon 'foo-equal
(foo-combine-values bar baz)))
Now there is a problem though... 'foo-equal
is now a symbol and only at runtime when executing foo-filter-by-function
a check whether 'foo-equal
is defined will be executed. Something that normally is a compile-time warning suddenly has become runtime-error. The same problem applies for builtin higher-order functions such as mapcar
, apply
...
Is there some possibility to make the byte compiler recognize, that e.g. the first argument to mapcar
should be a function and thus, when the argument is given as a symbol, check whether that symbol has a valid function definition, like it does with explicit (foo-equal ..)
calls?
Note: The purpose is to allow making production code more readable, so solutions that require a change of each call to foo-filter-by-function
or hackish code in its definition are not acceptable solutions. An example of such a counter-productive solution would be
(defun foo (bar baz)
(foo-filter-by-functon (lambda (a b) (foo-equal a b)) ... )
(foo-combine-values bar baz)))
or
(defun foo (bar baz)
(foo-filter-by-functon foo-equal
(foo-combine-values bar baz)))
(defconst foo-equal (lambda (a b) ...)
since both result in inferior code readability at least subjectively.
You want to use #'foo-equal
instead of 'foo-equal
so as to tell the compiler that you're not just talking about the foo-equal
symbol but about the foo-equal
function.