Function: thunk-let

thunk-let is a macro defined in thunk.el.gz.

Signature

(thunk-let BINDINGS &rest BODY)

Documentation

Like let but create lazy bindings.

BINDINGS is a list of elements of the form (SYMBOL EXPRESSION). Any binding EXPRESSION is not evaluated before the variable SYMBOL is used for the first time when evaluating the BODY.

It is not allowed to set thunk-let or thunk-let* bound variables.

Using thunk-let and thunk-let* requires lexical-binding.

View in manual

Probably introduced at or before Emacs version 27.1.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/thunk.el.gz
(defmacro thunk-let (bindings &rest body)
  "Like `let' but create lazy bindings.

BINDINGS is a list of elements of the form (SYMBOL EXPRESSION).
Any binding EXPRESSION is not evaluated before the variable
SYMBOL is used for the first time when evaluating the BODY.

It is not allowed to set `thunk-let' or `thunk-let*' bound
variables.

Using `thunk-let' and `thunk-let*' requires `lexical-binding'."
  (declare (indent 1) (debug let))
  (cl-callf2 mapcar
      (lambda (binding)
        (pcase binding
          (`(,(pred symbolp) ,_) binding)
          (_ (signal 'error (cons "Bad binding in thunk-let"
                                  (list binding))))))
      bindings)
  (cl-callf2 mapcar
      (pcase-lambda (`(,var ,binding))
        (list (make-symbol (concat (symbol-name var) "-thunk"))
              var binding))
      bindings)
  `(let ,(mapcar
          (pcase-lambda (`(,thunk-var ,_var ,binding))
            `(,thunk-var (thunk-delay ,binding)))
          bindings)
     (cl-symbol-macrolet
         ,(mapcar (pcase-lambda (`(,thunk-var ,var ,_binding))
                    `(,var (thunk-force ,thunk-var)))
                  bindings)
       ,@body)))