2009/10/7 Stephen C. Gilardi <squee...@mac.com> > On Oct 2, 2009, at 10:29 AM, Mark wrote: > > Is there a way to make a declaration in Clojure that cannot be rebound >> later? Ideally, I'd like something that fails if I try to do this: >> >> (def myname "mark") >> ; ...more code, elided... >> (def myname "Mark") >> > > Along these lines, I was thinking of adding defconst to > clojure.contrib.def. It's like Common Lisp's defconst, but the fact that its > value isn't allowed to change is enforced: > > (defmacro defconst > "Defines a var with a constant value and optional doc string. Any attempt > to redefine, bind, or set! the const to a different value will throw an > exception." > ([name init] > `(do (set-validator! (defvar ~name ~init) #{~init}) (var ~name))) > ([name init doc] > `(defconst ~(with-meta name (assoc (meta name) :doc doc)) ~init))) > > It does allow a redefine, binding, or set! to an equal value mainly as an > artifact of its implementation, but also because such a "change" would be > harmless to the semantics of it being constant. >
Harmless to the semantics of it being constant, yes, but not the semantics of it having to be defined just once ( and not having to be "tried" to be rebound ). e.g. if the new binding value is dynamic, the program may or may not work depending on the value ... I prefer Meikel's approach : the root binding is set once and for all, and that's the contrat. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---