1

I want to improve my Elisp, but despite reading about the differences between functions and macros (including discussions here in Emacs SE and on Reddit), I still find examples where the distinction confuses me.

For example, in the "starter-pack" Radian, a macro can be found which wraps use-package ensuring some sort of partial application:

(defmacro use-feature (name &rest args)
  "Like `use-package', but with `straight-use-package-by-default' disabled."
  (declare (indent defun))
  `(use-package ,name
     :straight nil
     ,@args))

As a case study, would it not be possible to achieve the same with a defun? After all, this seems like a simple case of partial application, or am I missing something? Do we need a macro here just because use-package itself is a macro which we don't want a defun body to evaluate?

And would there be a safe way to improve this macro to take care of a possibly unwanted case (use-feature foo :straight t)?

  • Macros can manage their raw arguments, while functions can't. If use-package is a functions, user will have to quote the arguments which is inconvenient and ugly, e.g., (use-package 'foo :if '(<= 1 2) :commands #'foo-bar :bind '("C-c C-c" . foo)), the same applies to use-feature, and if use-feature is not a macro, the use of it won't be like use-package. – xuchunyang Mar 25 at 13:54
  • 3
    What @xuchunyang said, where "raw" args means they are not evaluated when passed to the macro code. A macro can do anything it likes with/to any arg it is passed, including ignore it or evaluate it. A macro produces code, which is then evaluated. A macro can do anything with its args, but it is not about partial application per se. Partial application is about function application to fewer than all of the function's args. If an arg to a macro is a function then it can apply it partially to some args. Otherwise, there is no connection between partial eval and macros. – Drew Mar 25 at 14:05
  • 4
    As usual, a good way to learn about Elisp macros is to ask Emacs. C-h i, then choose Emacs Lisp Intro. In that intro-to-Elisp manual you'll find node Lisp macro, which will help. Or choose Elisp, the Emacs-Lisp reference and guide. There you'll find node Macros, which fills you in about Elisp macros. Much better than getting ad hoc answers here or on Reddit (IMHO). You're not the first person to learn Lisp macros - trust Emacs to help. – Drew Mar 25 at 14:45

Your Answer

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

Browse other questions tagged or ask your own question.