help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: [External] : Re: How do I pass a variable defined in a wrapping let,


From: Eduardo Ochs
Subject: Re: [External] : Re: How do I pass a variable defined in a wrapping let, to a lambda?
Date: Sat, 12 Mar 2022 19:33:45 -0300

On Sat, 12 Mar 2022 at 17:32, Drew Adams <drew.adams@oracle.com> wrote:
>
> > > `-*- lexical-binding:t -*-` doesn't prevent the use of
> > > dynamic scoping.
> >
> > How do I define a function that uses dynamic binding in a
> > file that has `-*- lexical-binding:t -*-`?
>
> A _function_ that uses it?  Just what do you mean by that?
>
> Function names are (typically) bound dynamically, with defun.
> ___
>
> For a variable, you use a vacuous defvar:
>
> (defvar foo) ; "special" var, i.e. dynamically bound

Hi Drew,

Here is something that I don't know how to port to lexical binding.
My functions that call `ee-template00' are all defined in files with
"-*- lexical-binding: nil; -*-" - they don't work if I put them in
files with "-*- lexical-binding: t; -*-".

  Cheers, E.


(defvar ee-template00-re "{\\([^{}]+\\)}")

;; Tests:
;; (ee-template00 "a{(+ 2 3)}b")
;; (let ((hi "Here: ") (a 22) (b 33)) (ee-template00 "{hi}{a}+{b}={(+ a b)}"))
;;
(defun ee-template00 (str)
  "Replace substrings enclosed by `{}'s in STR by the result of evaluating them.
Examples:\n
  (ee-template00 \"a{(+ 2 3)}b\")
    -->  \"a5b\"\n
  (let ((hi \"Here:\") (a 22) (b 33))
    (ee-template00 \"{hi} {a} + {b} = {(+ a b)}\"))
    -->  \"22 + 33 = 55\""
  (save-match-data
    (replace-regexp-in-string
     ee-template00-re
     (lambda (_code_) (format "%s" (eval (read (substring _code_ 1 -1)))))
     str 'fixedcase 'literal)))

;; Test:
;;   (ee-dynlex-test "Aa" "Bb")
;; The sexp above returns this with dynamic binding,
;;   "<Aa AaAa Bb BbBb>"
;; and yields this error in lexical binding:
;;   "format: Symbol's value as variable is void: a"
;;
(defun ee-dynlex-test (a b)
  (let* ((aa (concat a a))
         (bb (concat b b)))
    (ee-template00 "<{a} {aa} {b} {bb}>")))



reply via email to

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