all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Barry Margolin <barmar@alum.mit.edu>
To: help-gnu-emacs@gnu.org
Subject: Re: Writing a function/macro in Elisp that generates a function at runtime
Date: Tue, 28 Oct 2008 01:56:10 -0400	[thread overview]
Message-ID: <barmar-0CFC01.01561028102008@mara100-84.onlink.net> (raw)
In-Reply-To: mailman.2216.1225168079.25473.help-gnu-emacs@gnu.org

In article <mailman.2216.1225168079.25473.help-gnu-emacs@gnu.org>,
 Toby Cubitt <tsc25@cantab.net> wrote:

> Hi,
> 
> I'm trying to write a function in Elisp that generates a function at
> runtime, but hitting the limits of my Lisp abilities. Here's a first
> attempt at the kind of thing I'm trying to do:
> 
> (defun wrap-insert-function (insfun)
>   `(lambda (new-data old-cell)
>      (setf (cell-data old-cell)
>            (funcall ,insfun new-data (cell-data old-cell)))
>      old-cell))
> 
> I want to use this to wrap functions that don't know anything about the
> "cell" data structures, so they can operate on them:
> 
> (setq wrapped-insert-function
>   (wrap-insert-function
>    (some-complicated-code-that-returns-a-suitable-function)))
> 
> (funcall wrapped-insert-function data cell)
> 
> 
> The problem is, because it's quoted, the `setf' macro never gets
> expanded by the byte-compiler. And that's no good, because the cl
> package where `setf' is defined shouldn't be loaded at runtime, and

To do that you'd need lexical closures, but Elisp doesn't have them.  
Since you're not generating the function until runtime, there's nothing 
to compile at compile time.

> anyway we want it to be expanded at compile-time, not at run-time as
> that would be ugly and inefficient.

You don't need to generate a function on the fly for this.  Do something 
like this:

(defun insert-with-function (insfun new-data old-cell)
  (setf (cell-data old-cell)
        (funcall insfun new-data (cell-data old-cell)))
  old-cell)

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


       reply	other threads:[~2008-10-28  5:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2216.1225168079.25473.help-gnu-emacs@gnu.org>
2008-10-28  5:56 ` Barry Margolin [this message]
2008-10-28 10:38   ` Writing a function/macro in Elisp that generates a function at runtime Toby Cubitt
2008-10-28 12:42     ` Toby Cubitt
     [not found]     ` <mailman.2265.1225201433.25473.help-gnu-emacs@gnu.org>
2008-10-28 16:47       ` Ted Zlatanov
2008-10-28 17:10         ` Toby Cubitt
2008-10-27 13:30 Toby Cubitt
2008-10-28 18:09 ` Johan Bockgård
2008-10-28 17:21   ` Toby Cubitt

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

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

  git send-email \
    --in-reply-to=barmar-0CFC01.01561028102008@mara100-84.onlink.net \
    --to=barmar@alum.mit.edu \
    --cc=help-gnu-emacs@gnu.org \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.