[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Using funcall on inline functions
From: |
Stefan Monnier |
Subject: |
Re: Using funcall on inline functions |
Date: |
Sat, 12 Dec 2020 18:28:59 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
> (define-inline ebdb-add-to-list (list-var element)
> (inline-quote (when, element
> (cl-pushnew, element, list-var :test #'equal))))
This can't work: your argument is named "list-var" but the way you use
it in the body indicates that it's supposed to be a *list* not a *list
variable*.
It may happen to work in some cases when the function gets inlined, but
if so, it's actually showing a misfeature of the `define-inline`
implementation (result of some optimizations).
You can't have it both way: if `ebdb-add-to-list` is a function, then
`list-var` is a local variable and this argument uses the usual
pass-by-value semantics (and hence `cl-pushnew` will only affect that
local variable). If you want `list-var` to be the name of the *place*
passed by the caller then it has to be a macro.
There is a workaround, then, which is to use a reference:
(cl-defstruct ebdb-record-cache
(alt-names nil :type list))
(defclass ebdb-record ()
((cache :type ebdb-record-cache)))
(define-inline ebdb-record-alt-names (record)
(inline-quote (ebdb-record-cache-alt-names
(slot-value,record 'cache))))
(define-inline ebdb-add-to-list (list-ref element)
(inline-quote
(when ,element
(cl-pushnew ,element (gv-deref ,list-ref) :test #'equal))))
(let ((listfunc #'ebdb-add-to-list)
(name-string "Bob's new name"))
(funcall listfunc
(gv-ref (ebdb-record-alt-names <some-record>))
name-string))
[ BTW, your `ebdb-add-to-list` has a bug in that it will evaluate its
second argument before its first. ]
Stefan