[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Using funcall on inline functions
From: |
Eric Abrahamsen |
Subject: |
Re: Using funcall on inline functions |
Date: |
Sun, 13 Dec 2020 09:48:17 -0800 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> (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.
Thanks for the detailed explanation; I see what the problem is.
> 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))
Hmm, interesting. I guess I would feel a little weird about having to
change all callers specifically for this -- I can see myself six months
from now having no memory of doing this, and causing bugs for myself.
But this definitely helps clarify the problem.
> [ BTW, your `ebdb-add-to-list` has a bug in that it will evaluate its
> second argument before its first. ]
inline-letevals! But, for my information, isn't the bug that ELEMENT is
evaluated twice? Why does the order of evaluation matter?
Thanks,
Eric