[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#69521: 29.1; Adding a DECLARE spec equivalent to DECLARE-FUNCTION
From: |
Adam Porter |
Subject: |
bug#69521: 29.1; Adding a DECLARE spec equivalent to DECLARE-FUNCTION |
Date: |
Sun, 3 Mar 2024 04:49:02 -0600 |
User-agent: |
Mozilla Thunderbird |
Hi,
In light of [this thread], this seems like a good time to propose this
idea: to enhance the `declare' form with a `function' spec equivalent to
the top-level `declare-function' form. This allows keeping the
declaration inside the function that makes it necessary, which has a few
advantages:
1. It clearly indicates which function makes the declaration necessary
(i.e. which function contains calls to the function in another
library).
2. If the function is moved to another file, the declaration moves with
it.
3. If the function is removed, the declaration is removed with it.
4. It reduces the number of top-level forms.
Also, it seems like a natural fit, i.e. `declare-function' to `(declare
(function ...))'.
As an experiment, I implemented this in [Ement] about 10 months ago, and
I've neither seen nor heard of any problems with it. Here's the code
that implements it:
┌────
│ (eval-and-compile
│ (defun ement--byte-run--declare-function (_name _args &rest values)
│ "Return a `declare-function' form with VALUES.
│ Allows the use of a form like:
│
│ (declare (function FN FILE ...))
│
│ inside of a function definition, effectively keeping its
│ `declare-function' form inside the function definition, ensuring
│ that stray such forms don't remain if the function is removed."
│ `(declare-function ,@values))
│
│ (cl-pushnew '(function ement--byte-run--declare-function)
defun-declarations-alist :test #'equal)
│ (cl-pushnew '(function ement--byte-run--declare-function)
macro-declarations-alist :test #'equal))
└────
In use, it looks like this:
┌────
│ (defun ement-notifications--make (notification)
│ "Return an `ement-notification' struct for NOTIFICATION.
│ NOTIFICATION is an alist representing a notification returned
│ from the \"/notifications\" endpoint. The notification's event
│ is passed through `ement--make-event'."
│ (declare (function ement--make-event "ement"))
│ (pcase-let (((map room_id _actions _ts event read) notification))
│ (make-ement-notification :room-id room_id :readp read
│ :event (ement--make-event event))))
└────
Would this be an acceptable addition to Emacs Lisp? If so, I'd be glad
to prepare a patch.
Thanks, Adam
[this thread]
<https://lists.gnu.org/archive/html/emacs-devel/2024-03/msg00057.html>
[Ement] <https://elpa.gnu.org/packages/ement.html>
- bug#69521: 29.1; Adding a DECLARE spec equivalent to DECLARE-FUNCTION,
Adam Porter <=