all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* binding question
@ 2013-10-06  9:35 Eric Abrahamsen
  2013-10-06 10:14 ` Thien-Thi Nguyen
  2013-10-06 12:39 ` Michael Heerdegen
  0 siblings, 2 replies; 5+ messages in thread
From: Eric Abrahamsen @ 2013-10-06  9:35 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-10-06 14:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-06  9:35 binding question Eric Abrahamsen
2013-10-06 10:14 ` Thien-Thi Nguyen
2013-10-06 14:03   ` Eric Abrahamsen
2013-10-06 14:45   ` Stefan Monnier
2013-10-06 12:39 ` Michael Heerdegen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.