all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Oleh Krehel <ohwoeowho@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: Use the new let-opt macro in place of pcase-let in lisp-mode.el
Date: Mon, 18 May 2015 19:26:29 +0200	[thread overview]
Message-ID: <87oalh6blm.fsf@gmail.com> (raw)
In-Reply-To: <jwvwq05x2zq.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Mon, 18 May 2015 12:39:57 -0400")

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> (defmacro let-opt (bindings &rest body)
>>   "Like `let', but allows for compile time optimization.
>> Expressions wrapped with `opt' will be subsituted for their values.
>> \n(fn BINDINGS BODY)"
>>   (declare (indent 1) (debug let))
>>   (let ((bnd (mapcar (lambda (x) (cons (car x) (eval (cadr x))))
>>                      bindings)))
>>     `(cl-macrolet ((opt (&rest body)
>>                         (list 'quote (eval (cons 'progn body) ',bnd))))
>>        ,@body)))
>
> I think I like this idea of "compile-time-only let-binding".
> But I don't like this `opt' thingy very much and I think we can get rid
> of it if we use dynamic-scoping instead.
>
> IOW, define a let-when-compile macro which uses progv to setup the
> bindings and then calls `macroexpand-all' on the body.  The body's
> `eval-when-compile' can then use those vars just fine.

I wrote this:

(defmacro let-when-compile (bindings &rest body)
  "Like `let', but allows for compile time optimization.
\n(fn BINDINGS BODY)"
  (declare (indent 1) (debug let))
  `(progv ',(mapcar #'car bindings)
       ',(mapcar (lambda (x) (eval (cadr x))) bindings)
     ,@body))

It sort of works (evals to the correct thing, and the byte code looks
OK), but I get a lot of byte compiler warnings. And I don't see while
`progv' should expand to a `while' loop. Anyway, here's an example:

(let-when-compile ((foo (+ 2 2)))
  (defvar bar (+ foo foo)))

And here's the expansion, after I've evaluated the while loop:

(eval
 (let ((foo (quote 4)))
   (funcall
    (quote
     (lambda nil
       (defvar bar (+ foo foo)))))))

On the other hand, using `let-opt':

(let-opt ((foo (+ 2 2)))
  (defvar bar (opt (+ foo foo))))

expands to this:

(defvar bar '8)

Which I think could be valuable while refactoring.

Oleh



  reply	other threads:[~2015-05-18 17:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-18 15:16 Use the new let-opt macro in place of pcase-let in lisp-mode.el Oleh Krehel
2015-05-18 16:39 ` Stefan Monnier
2015-05-18 17:26   ` Oleh Krehel [this message]
2015-05-19  1:11     ` Stefan Monnier
2015-05-19  8:08       ` Oleh Krehel
2015-05-20  2:03         ` Stefan Monnier
2015-05-20  8:46           ` Oleh Krehel
2015-05-20 12:58             ` Stefan Monnier
2015-05-20 14:28               ` Oleh Krehel
2015-05-20 18:48                 ` Stefan Monnier

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=87oalh6blm.fsf@gmail.com \
    --to=ohwoeowho@gmail.com \
    --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 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.