unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: Using funcall on inline functions
Date: Sun, 13 Dec 2020 09:48:17 -0800	[thread overview]
Message-ID: <87h7opzjfi.fsf@ericabrahamsen.net> (raw)
In-Reply-To: jwvr1numx0l.fsf-monnier+emacs@gnu.org

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



  reply	other threads:[~2020-12-13 17:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-12 22:49 Using funcall on inline functions Eric Abrahamsen
2020-12-12 23:28 ` Stefan Monnier
2020-12-13 17:48   ` Eric Abrahamsen [this message]
2020-12-13 18:12     ` Stefan Monnier
2020-12-13 18:27       ` Eric Abrahamsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h7opzjfi.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).