emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use the new let-opt macro in place of pcase-let in lisp-mode.el


From: Oleh Krehel
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
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Stefan Monnier <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]