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

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

Re: the (declare special) declaration with lexical scope.


From: Platon Pronko
Subject: Re: the (declare special) declaration with lexical scope.
Date: Sun, 23 Apr 2023 17:47:40 +0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1

I'm not qualified to answer the question about lexical scope.

However I'm interested why you didn't consider to make the entire mustache 
function
into a macro that expands to concat call with variables already interpolated?
This way it will work regardless of lexical or dynamic binding.

I suppose this might break if template is dynamically generated somewhere,
however templates are usually static and thus can be embedded at compile time
(it will be the fastest-performing option, aside from other benefits).

Here's an example of what I mean:

(defun mustache-build (template)
  (let ((pattern-start (string-match "{{\\(.*?\\)}}" template)))
    (if (not pattern-start)
        template
      `(,(substring template 0 pattern-start)
        (prin1-to-string ,(intern (match-string 1 template)))
        ,@(if (>= (match-end 0) (length template)) nil (mustache-build 
(substring template (match-end 0))))))))
(defmacro mustache (template)
  `(concat ,@(mustache-build template)))

(macroexpand '(mustache "a={{a}} b={{b}}"))
; (concat "a=" (prin1-to-string a) " b=" (prin1-to-string b))

(let ((a 42) (b '(+ 2 3)))
  (mustache "a={{a}} b={{b}}"))
; "a=42 b=(+ 2 3)"

--
Best regards,
Platon Pronko
PGP 2A62D77A7A2CB94E



reply via email to

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