Re: immutable defs?

2009-10-07 Thread John Harrop
On Tue, Oct 6, 2009 at 9:14 PM, Stephen C. Gilardi squee...@mac.com wrote: `(do (set-validator! (defvar ~name ~init) #{~init}) (var ~name))) Cute hack. Won't work if init is false or nil, though, unless the validator does not trigger on the initial assignment of the value.

Re: immutable defs?

2009-10-07 Thread Meikel Brandmeyer
Hi, On Oct 7, 7:53 am, John Harrop jharrop...@gmail.com wrote: On Tue, Oct 6, 2009 at 9:14 PM, Stephen C. Gilardi squee...@mac.com wrote:     `(do (set-validator! (defvar ~name ~init) #{~init}) (var ~name))) Cute hack. Won't work if init is false or nil, though, unless the validator does

Re: immutable defs?

2009-10-07 Thread Laurent PETIT
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)

Re: immutable defs?

2009-10-07 Thread Laurent PETIT
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)

Re: immutable defs?

2009-10-07 Thread Stephen C. Gilardi
On Oct 7, 2009, at 5:19 AM, Meikel Brandmeyer wrote: Or is setting the validator calling it on the already set value? Yes, the validation mechanism calls the validator on the already set value. --Steve smime.p7s Description: S/MIME cryptographic signature

Re: immutable defs?

2009-10-07 Thread Stephen C. Gilardi
On Oct 7, 2009, at 12:47 AM, Mark Tomko wrote: This is pretty much what I'd had in mind. Thanks for the comments and suggestions all. I don't see how the text of the Exception is set by the macro, but it'd be really spectacular if the message were more clear. Is that message coming from

Re: immutable defs?

2009-10-06 Thread wschnell
from the API docs: (defonce myname Walter) ; ... (defonce myname Schnell) = nil myname = Walter hope this helps ;-) On 2 Okt., 16:29, Mark mjt0...@gmail.com 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

Re: immutable defs?

2009-10-06 Thread Travis
+1 In my relatively novice opinion, unless there is a reason to make functions and vars available to code executing in a *different* namespace, there isn't a lot of reason to def anything at all. On Oct 2, 11:48 am, Jonathan Smith jonathansmith...@gmail.com wrote: I use a let at the top of the

Re: immutable defs?

2009-10-06 Thread Stephen C. Gilardi
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

Re: immutable defs?

2009-10-06 Thread rzeze...@gmail.com
On Oct 2, 11:52 am, Mark Tomko mjt0...@gmail.com wrote: However, outside the scope of a function, it seems that it's possible for bindings to be redefined later in a file without causing an immediate error.  This could easily lead to mistakes that would manifest as silent and potentially

Re: immutable defs?

2009-10-06 Thread Mark Tomko
This is pretty much what I'd had in mind. I don't see how the text of the Exception is set by the macro, but it'd be really spectacular if the message were more clear. Is that message coming from the defvar form? On Oct 6, 6:14 pm, Stephen C. Gilardi squee...@mac.com wrote: On Oct 2, 2009, at

immutable defs?

2009-10-02 Thread Mark
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) Perhaps this is obvious, but I see a lot of discussion of immutable data structures, but I

Re: immutable defs?

2009-10-02 Thread Laurent PETIT
I can think of 'defvar that comes close to what you're about : it does not rebind the root value if it is different from nil (though it doesn't warn you about the problem, it's more to prevent reinitializing vars when their containing files are reloaded) 2009/10/2 Mark mjt0...@gmail.com Is

Re: immutable defs?

2009-10-02 Thread Meikel Brandmeyer
Hi, On Oct 2, 4:29 pm, Mark mjt0...@gmail.com 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) Perhaps this is obvious, but I

Re: immutable defs?

2009-10-02 Thread Mark Tomko
When I write code in Java, I declare everything final that's possible to be declared final, and I deliberately look for solutions that avoid reassignment to variables, so all my variables are final). I'm new to Clojure, so I might be wrong, but it seems that within a function, mutable bindings

Re: immutable defs?

2009-10-02 Thread Stuart Sierra
On Oct 2, 11:52 am, Mark Tomko mjt0...@gmail.com wrote: However, outside the scope of a function, it seems that it's possible for bindings to be redefined later in a file without causing an immediate error.  This could easily lead to mistakes that would manifest as silent and potentially

Re: immutable defs?

2009-10-02 Thread John Newman
From what I've seen, people never redef vars in source code. In general, you shouldn't have to worry about users of your code redefing your vars as it's against common convention, non-idiomatic. The exception, as Stuart Sierra said, is when writing interactively from the REPL, where you'd like

Re: immutable defs?

2009-10-02 Thread John Newman
Also, I'm not sure if your understanding of binding is correct. because within any lexical scope inside a function, I can pretty much count on my bindings to never change. binding is actually like a lexical re-def: user= (def x 1) #'user/x user= (binding [x 2] (pr x) (binding [x 3] (pr x))

Re: immutable defs?

2009-10-02 Thread Jonathan Smith
I use a let at the top of the file to denote things that I want to have as captured and constant. ... you can do things like (let [x 1] (defn foo-that-uses-x [y] (function-here x y))) On Oct 2, 10:29 am, Mark mjt0...@gmail.com wrote: Is there a way to make a declaration in Clojure that

Re: immutable defs?

2009-10-02 Thread Mark Tomko
That's what I meant when I mentioned the 'binding' form above. The reason that's okay (to me) is that it explicitly calls out that bindings may be about to change. On Oct 2, 11:47 am, John Newman john...@gmail.com wrote: Also, I'm not sure if your understanding of binding is correct. because