[Top][All Lists]

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

Re: Why is FUNC in cl-callf not allowed to be an expression?

From: Michael Heerdegen
Subject: Re: Why is FUNC in cl-callf not allowed to be an expression?
Date: Sat, 18 May 2019 00:53:10 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

> So I think it needs a name which clarifies the difference.

Hmm, ok.  Here is a draft supporting multiple (parallel) bindings.  The
macro builds code by recursively calling `gv-letplace', the innermost
expression is one big symbol-macrolet that makes use of the bound
getters and setters.

#+begin_src emacs-lisp
(defmacro gv-place-bind (bindings &rest body)
  "Make place expression bindings.
BINDINGS is a list of elements of the form (VAR PLACE).

Eval BODY with...

The effect is very similar to `cl-symbol-macrolet'
but preferred for place expressions since it produces more
efficient code.

\(fn ((VAR PLACE) ...) FORM...)"
  (declare (indent 1))
  (letrec ((helper
            (lambda (bindings symbols+getters+setters body)
              (if bindings
                  (let ((binding (car bindings)))
                    (gv-letplace (getter setter) (cadr binding)
                      (funcall helper
                               (cdr bindings)
                               (cons (list (car binding) getter setter)
                       (lambda (entry)
                         `(,(car entry)
                           (gv-synthetic-place ,(cadr entry) ,(caddr entry))))
                       (nreverse symbols+getters+setters))
    (funcall helper bindings '() body)))

;; Example:
(let ((l '(2 3 4)))
    (gv-place-bind ((p (cdr l)))
      (unless (memq 1 p)
        (setf p (cons 1 p))))
;; ==> (2 1 3 4)

Writing the docstring made me thoughtful though - how is this different
from symbol-macrolet?  An advantage is that it generates a bit more
efficient code for "complicated" (nested) place expressions.

OTOH, the purpose of symbol-macrolet is, at the end, more or less
defining abbreviations of place expressions.  So I wonder now if the
right thing to do is rather to improve symbol-macrolet instead to make
it generate better code by consulting getters and setters itself,
instead of blindly substituting.

My second point in this message: thinking once more about callf, we
could also support a syntax like (callf (with EXPR) PLACE ARG) or
something like that to support expressions as first arg.


reply via email to

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