Re: Strange behaviour: nested def in proxy method body

2012-12-14 Thread kristianlm

Ah, thanks for your reply, both of you!

That's why I got confused: it seemed to work when I didn't use proxy. I'll 
stop using nested defs for now. But I will miss them: they allow me to 
evaluate them top-level in the REPL (using C-x C-e in emacs) and play 
around with them.

K.


On Thursday, December 13, 2012 7:56:50 PM UTC+1, Alan Malloy wrote:

 On Thursday, December 13, 2012 4:14:23 AM UTC-8, Marshall 
 Bockrath-Vandegrift wrote:

 kristianlm kris...@adellica.com writes: 

  I'm enjoying testing Java code with Clojure and it's been a lot of fun 
  so far. Coming from Scheme, the transit is comfortable. However, I 
  encountered a big surprise when I nested def's and used them with a 
  proxy: 

 This is a common surprise for people with previous exposure to Scheme: 
 `def` in Clojure is always explicitly namespace-scoped.  What it does is 
 create a var with a name and intern it into the namespace with that 
 name, not introduce a name in the current lexical scope.  Nested `def`s 
 are thus very rarely going to be what one actually want, and the 
 behavior you’re seeing is exactly what one would expect. 


 To further clarify: the mew* version doesn't *actually* behave just like 
 new-let*, although it usually will have the same consequences. The issue is 
 that there's a race condition if two different threads call mew* at the 
 same time: they might both mutate i, then both read it, and thus end up 
 with the same reference. Really, as Marhsall says, a nested def is (almost) 
 never what you want. 


-- 
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

Strange behaviour: nested def in proxy method body

2012-12-13 Thread kristianlm

Hello Clojurers!

I'm enjoying testing Java code with Clojure and it's been a lot of fun so 
far. Coming from Scheme, the transit is comfortable. However, I encountered 
a big surprise when I nested def's and used them with a proxy:

(defn new* []
  (def i (atom  0))
  (proxy [Object] []
(toString [] (str i

(defn new*-let []
  (let [i (atom  0)]
(proxy [Object] []
  (toString [] (str i)

(defn mew* []
  (def i (atom 0))
  i)

(pprint (list (new*) (new*) (new*)
  (new*-let) (new*-let) (new*-let) 
  (mew*) (mew*) (mew*)))

I expected to see a list of 3 * 3 elements, each element a unique Atmon 
instance. But from it's printout, the first three instances are identical. 
Shouldn't all versions of new* produce their own version of a 
lexically-scoped i (like in Scheme), or am I missing something? Are nested 
defs and lets not supposed to behave identically?

Note that the non-proxied version, mew*, makes nested def work like let. 
Could this be a proxy-bug?

K.

-- 
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

Re: Strange behaviour: nested def in proxy method body

2012-12-13 Thread Marshall Bockrath-Vandegrift
kristianlm krist...@adellica.com writes:

 I'm enjoying testing Java code with Clojure and it's been a lot of fun
 so far. Coming from Scheme, the transit is comfortable. However, I
 encountered a big surprise when I nested def's and used them with a
 proxy:

This is a common surprise for people with previous exposure to Scheme:
`def` in Clojure is always explicitly namespace-scoped.  What it does is
create a var with a name and intern it into the namespace with that
name, not introduce a name in the current lexical scope.  Nested `def`s
are thus very rarely going to be what one actually want, and the
behavior you’re seeing is exactly what one would expect.

-Marshall

-- 
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


Re: Strange behaviour: nested def in proxy method body

2012-12-13 Thread Alan Malloy
On Thursday, December 13, 2012 4:14:23 AM UTC-8, Marshall 
Bockrath-Vandegrift wrote:

 kristianlm kris...@adellica.com javascript: writes: 

  I'm enjoying testing Java code with Clojure and it's been a lot of fun 
  so far. Coming from Scheme, the transit is comfortable. However, I 
  encountered a big surprise when I nested def's and used them with a 
  proxy: 

 This is a common surprise for people with previous exposure to Scheme: 
 `def` in Clojure is always explicitly namespace-scoped.  What it does is 
 create a var with a name and intern it into the namespace with that 
 name, not introduce a name in the current lexical scope.  Nested `def`s 
 are thus very rarely going to be what one actually want, and the 
 behavior you’re seeing is exactly what one would expect. 


To further clarify: the mew* version doesn't *actually* behave just like 
new-let*, although it usually will have the same consequences. The issue is 
that there's a race condition if two different threads call mew* at the 
same time: they might both mutate i, then both read it, and thus end up 
with the same reference. Really, as Marhsall says, a nested def is (almost) 
never what you want. 

-- 
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