Re: clojurescript closure problem?

2011-09-22 Thread Mark Nutter
I stand corrected. I guess I'm *too* familiar with JS. I'll have to re-learn
a few things.

m

On Thu, Sep 22, 2011 at 8:34 AM, Meikel Brandmeyer (kotarak) 
wrote:

> Hi,
>
> Disclaimer: I haven't touched ClojureScript a single time up to now, I only
> know the talk of Rich about it. Neither am I JavaScript developer.
>
> Too me this smells like a bug. ClojureScript is not JavaScript. It tries to
> bring (where possible) Clojure semantics to the JavaScript VM. The first
> version is perfectly valid Clojure and I would be very surprised if this
> wouldn't work the same way in ClojureScript. If such radically different
> behaviour is to be expected, then I understood something seriously wrong
> about ClojureScript.
>
> In particular it kills sharing of "clojure only" libraries between both
> target platforms.
>
> Sincerely
> Meikel
>
>
>  --
> 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
>

-- 
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: clojurescript closure problem?

2011-09-22 Thread David Nolen
On Thu, Sep 22, 2011 at 11:12 AM, Michael Fogus  wrote:

> > the body of loop in a function to preserve bindings if other fns are
> > discovered that close over them so as to avoid introducing a perf hit.
>
> It would be interesting to see if GClosure is smart enough to deal
> with the naive implementation.  I know what I'll experiment on
> tomorrow.  :-)


On further consideration I think it's more desirable to wrap the fns at
their declaration site in a function which is immediately called to preserve
the loop bindings. No need for the entire loop body to pay for preserving
locals.

David

-- 
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: clojurescript closure problem?

2011-09-22 Thread Michael Fogus
> the body of loop in a function to preserve bindings if other fns are
> discovered that close over them so as to avoid introducing a perf hit.

It would be interesting to see if GClosure is smart enough to deal
with the naive implementation.  I know what I'll experiment on
tomorrow.  :-)

-- 
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: clojurescript closure problem?

2011-09-22 Thread David Nolen
There's a patch there but I think it's too simplistic. It should only wrap
the body of loop in a function to preserve bindings if other fns are
discovered that close over them so as to avoid introducing a perf hit.

David

On Thu, Sep 22, 2011 at 8:46 AM, David Nolen  wrote:

> It's a known issue:
>
> http://dev.clojure.org/jira/browse/CLJS-39
>
> David
>
> On Wed, Sep 21, 2011 at 2:34 PM, Eric Harris-Braun <
> zippy.314@gmail.com> wrote:
>
>> Check out this little bit of code:
>>
>> (doseq [hid ["a" "b" "c"]]
>>  (goog.dom.appendChild (goog.dom.$ "some-element-id")
>> (goog.dom.createDom "div" (.strobj {"id" hid})  (str "Test-"hid)))
>>  (goog.events.listen (goog.dom.$ hid) goog.events.EventType.CLICK,
>> (fn [e] (js/alert hid
>>
>> What I want it do to is add in a few div's that when clicked on simply
>> alert with the value their id value.  That doesn't actually happen,
>> what happens is that they all alert with the "c" ie. the last value in
>> the list.  This code, on the other-hand works:
>>
>> (defn hidfn [hid]
>>  (fn [e] (js/alert hid))
>>  )
>> (doseq [hid ["a" "b" "c"]]
>>  (goog.dom.appendChild (goog.dom.$ "the-receptor")
>> (goog.dom.createDom "div" (.strobj {"id" hid})  (str "Test-"hid)))
>>  (goog.events.listen (goog.dom.$ hid) goog.events.EventType.CLICK,
>> (hidfn hid)))
>>
>> Shouldn't the clojurescript compiler detect that the usage of "hid" in
>> the function in the first case requires the creation of an anonymous
>> function to close around that value?  Why do I have to do it manually?
>>
>> --
>> 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
>
>
>

-- 
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: clojurescript closure problem?

2011-09-22 Thread Chouser
On Thu, Sep 22, 2011 at 8:46 AM, David Nolen  wrote:
> It's a known issue:
> http://dev.clojure.org/jira/browse/CLJS-39

I somehow missed that one and had filed this one as well:

http://dev.clojure.org/jira/browse/CLJS-59

--Chouser

-- 
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: clojurescript closure problem?

2011-09-22 Thread David Nolen
It's a known issue:

http://dev.clojure.org/jira/browse/CLJS-39

David

On Wed, Sep 21, 2011 at 2:34 PM, Eric Harris-Braun
wrote:

> Check out this little bit of code:
>
> (doseq [hid ["a" "b" "c"]]
>  (goog.dom.appendChild (goog.dom.$ "some-element-id")
> (goog.dom.createDom "div" (.strobj {"id" hid})  (str "Test-"hid)))
>  (goog.events.listen (goog.dom.$ hid) goog.events.EventType.CLICK,
> (fn [e] (js/alert hid
>
> What I want it do to is add in a few div's that when clicked on simply
> alert with the value their id value.  That doesn't actually happen,
> what happens is that they all alert with the "c" ie. the last value in
> the list.  This code, on the other-hand works:
>
> (defn hidfn [hid]
>  (fn [e] (js/alert hid))
>  )
> (doseq [hid ["a" "b" "c"]]
>  (goog.dom.appendChild (goog.dom.$ "the-receptor")
> (goog.dom.createDom "div" (.strobj {"id" hid})  (str "Test-"hid)))
>  (goog.events.listen (goog.dom.$ hid) goog.events.EventType.CLICK,
> (hidfn hid)))
>
> Shouldn't the clojurescript compiler detect that the usage of "hid" in
> the function in the first case requires the creation of an anonymous
> function to close around that value?  Why do I have to do it manually?
>
> --
> 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

-- 
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: clojurescript closure problem?

2011-09-22 Thread Meikel Brandmeyer (kotarak)
Hi,

Disclaimer: I haven't touched ClojureScript a single time up to now, I only 
know the talk of Rich about it. Neither am I JavaScript developer.

Too me this smells like a bug. ClojureScript is not JavaScript. It tries to 
bring (where possible) Clojure semantics to the JavaScript VM. The first 
version is perfectly valid Clojure and I would be very surprised if this 
wouldn't work the same way in ClojureScript. If such radically different 
behaviour is to be expected, then I understood something seriously wrong 
about ClojureScript.

In particular it kills sharing of "clojure only" libraries between both 
target platforms.

Sincerely
Meikel

-- 
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: clojurescript closure problem?

2011-09-22 Thread Mark Nutter
No, because that might not be what you want your code to do. As a JavaScript
programmer you have to understand what function scope means, and how to use
it appropriately. If "hid" is a variable that's in scope when you define an
anonymous function, your functions will refer to that specific variable. If
you define multiple anonymous functions in the same scope, they will
likewise all reference the same specific variable (and sometimes that's what
you want).  If you want each new anonymous function to have its own separate
variable named "hid", then the way you tell the compiler what you want is by
wrapping another function around the code that generates your anonymous
function, and passing "hid" in as the argument to the wrapper function.
Having the compiler try and second-guess what it thinks you *meant* would be
bad, because someday some JavaScript programmer would try to share a
variable between multiple anonymous functions, and would be scratching their
head trying to figure out why the "smart" compiler was failing to obey the
rules of JavaScript function scope.

m

On Wed, Sep 21, 2011 at 2:34 PM, Eric Harris-Braun
wrote:

>
> Shouldn't the clojurescript compiler detect that the usage of "hid" in
> the function in the first case requires the creation of an anonymous
> function to close around that value?  Why do I have to do it manually?
>
>

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

clojurescript closure problem?

2011-09-21 Thread Eric Harris-Braun
Check out this little bit of code:

(doseq [hid ["a" "b" "c"]]
  (goog.dom.appendChild (goog.dom.$ "some-element-id")
(goog.dom.createDom "div" (.strobj {"id" hid})  (str "Test-"hid)))
  (goog.events.listen (goog.dom.$ hid) goog.events.EventType.CLICK,
(fn [e] (js/alert hid

What I want it do to is add in a few div's that when clicked on simply
alert with the value their id value.  That doesn't actually happen,
what happens is that they all alert with the "c" ie. the last value in
the list.  This code, on the other-hand works:

(defn hidfn [hid]
  (fn [e] (js/alert hid))
  )
(doseq [hid ["a" "b" "c"]]
  (goog.dom.appendChild (goog.dom.$ "the-receptor")
(goog.dom.createDom "div" (.strobj {"id" hid})  (str "Test-"hid)))
  (goog.events.listen (goog.dom.$ hid) goog.events.EventType.CLICK,
(hidfn hid)))

Shouldn't the clojurescript compiler detect that the usage of "hid" in
the function in the first case requires the creation of an anonymous
function to close around that value?  Why do I have to do it manually?

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