emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can the byte-compiler check whether functions passed by name are def


From: Klaus-Dieter Bauer
Subject: Re: Can the byte-compiler check whether functions passed by name are defined?
Date: Wed, 31 Jul 2013 15:44:39 +0200

2013/7/29 Stefan Monnier <address@hidden>
>     (mapcar 'my-function-1 some-list)?

We could do it for symbols quoted with #' but not for quoting with '.

        Stefan

#' would allow checking for a defined function independent of the function, the function is oassed to, true... Would require however to change coding practice to using this syntax, with the advantage of preventing warnings when people don't want the check but the disadvantage, that the warning is also supressed when people just don't care. I guess it would still be the best solution, that is backward compatible though. 

Another possibilty (probably less good though):

    (eval-and-compile 
      (put 'mapcar 'compiler-macro 
           (lambda (&rest form)
             (let ((function (nth 1 form)))
               (when (and (macroexp--compiling-p)
         (listp function)
                          (= 2 (length function))
                          (member (nth 0 function) '(function quote))
                          (symbolp (nth 1 function))
                          (not (fboundp (nth 1 function))))
                 (warn "Function not know to be defined: %S" (nth 1 function))))
    (car form))))
    
    (mapcar 'message '("Hello" "World"))
    (mapcar 'foobar '("Foo" "Bar"))

Some caveats here:
1. Has to be done, though maybe through a macro, for every higher-order function.
2. `compiler-macro' doesn't seem to be documented anywhere. The arguments the function gets passed seem to differ from case to case... 
3. (warn) doesn't emit warnings to the compilation buffer but to the separate *Warnings* buffer, making this code only a prrof-of-concept without practical value.
4. If the quoted funciton is defined in the same file as the higher-order function it is passed to, the definition of the quoted function must be both before the first use of the function and inside an (eval-and-compile ..) block, which can only be prevented by changes to the compiler code anyway.

kind regards, Klaus

reply via email to

[Prev in Thread] Current Thread [Next in Thread]