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

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

binding question


From: Eric Abrahamsen
Subject: binding question
Date: Sun, 06 Oct 2013 17:35:30 +0800
User-agent: Gnus/5.130008 (Ma Gnus v0.8) Emacs/24.3 (gnu/linux)

I've got a variable binding scenario that I thought would be easy to
resolve, but is giving me trouble. I have the feeling that this scenario
represents some sort of Lesson 1 in How Bindings Work, but I'm still not
sure of the proper solution.

The general situation is: I want to start with a list template stored in
a variable. Another function in this library starts with the template,
modifies it a few times, then does something with the modified version.

Every time this function is called, it should start with a "fresh" copy
of the template.

Clearly the following doesn't work: it actually modifies the original
list-template.

(defvar list-template
  '(item ((with . "some")
          (sub . "lists"))
         nil (other things will go here)))

(defun main-entry-function ()
  ;; obviously this doesn't work as defvars are always special
  (let ((list-template list-template))
  ;; list-template is accessed dynamically in the following functions
    (modify-list-template)
    (modify-list-template-again)
    (do-something-with-modified-list-template)))

The other option would be to pass list-template as an argument to each
of these functions. I guess each call would then have to look like:

(setq list-template (modify-list-template list-template))

This seems ugly, and it won't necessarily be easy to ensure the
modifying functions end by returning the new value of list-template.

What's the best way of handling this? And is there one "right" solution
for both dynamic and lexical binding?

Thanks!
Eric


PS: The wiki[1] mentions pretty much exactly my scenario, but doesn't
actually say what should be done.

[1]
http://www.emacswiki.org/emacs/DynamicBindingVsLexicalBinding#toc4




reply via email to

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