Re: Defining a namespace inside a let
On May 2, 3:09 pm, alux wrote: > Hm. Can you point me to some documentation about these special rules > then? Some on http://clojure.org/namespaces But the best rule of thumb is: never use "ns" or "in-ns" anywhere except at the top of a source file. -S -- 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: Defining a namespace inside a let
Hello Stuart, "they don't work as you'd expect". Ah, I see. Thank you ;-) Hm. Can you point me to some documentation about these special rules then? Many thanks, alux On 30 Apr., 18:10, Stuart Sierra wrote: > "ns" and "in-ns" have special evaluation rules. In general, they > don't work as you'd expect in block expressions such as "do" or "let". > > If you want to create namespaces programatically, use "create-ns" and > "intern". > > -SS > > On Apr 26, 6:25 pm, David McNeil wrote: > > > > > I am experimenting with clojure.test and I encountered the following > > situation which I cannot explain. > > > This code: > > > (println (do > > (ns ns01 > > (:use clojure.test)) > > (deftest test1 nil) > > (run-tests))) > > > Produces the expected result (note: it runs one test): > > > Testing ns01 > > > Ran 1 tests containing 0 assertions. > > 0 failures, 0 errors. > > {:type :summary, :test 1, :pass 0, :fail 0, :error 0} > > > However, if I do the exact same thing inside of a let: > > > (println (let [] > > (do > > (ns ns02 > > (:use clojure.test)) > > (deftest test1 nil) > > (run-tests > > > Then I get the unexpected result that no tests are executed: > > > Testing ns02 > > > Ran 0 tests containing 0 assertions. > > 0 failures, 0 errors. > > {:type :summary, :test 0, :pass 0, :fail 0, :error 0} > > > Seems there is something going on with namespaces that I do not > > understand and I hope that somewhere here can explain it. > > > Thank you. > > -David McNeil > > > -- > > 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 > > athttp://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 > athttp://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: Defining a namespace inside a let
"ns" and "in-ns" have special evaluation rules. In general, they don't work as you'd expect in block expressions such as "do" or "let". If you want to create namespaces programatically, use "create-ns" and "intern". -SS On Apr 26, 6:25 pm, David McNeil wrote: > I am experimenting with clojure.test and I encountered the following > situation which I cannot explain. > > This code: > > (println (do > (ns ns01 > (:use clojure.test)) > (deftest test1 nil) > (run-tests))) > > Produces the expected result (note: it runs one test): > > Testing ns01 > > Ran 1 tests containing 0 assertions. > 0 failures, 0 errors. > {:type :summary, :test 1, :pass 0, :fail 0, :error 0} > > However, if I do the exact same thing inside of a let: > > (println (let [] > (do > (ns ns02 > (:use clojure.test)) > (deftest test1 nil) > (run-tests > > Then I get the unexpected result that no tests are executed: > > Testing ns02 > > Ran 0 tests containing 0 assertions. > 0 failures, 0 errors. > {:type :summary, :test 0, :pass 0, :fail 0, :error 0} > > Seems there is something going on with namespaces that I do not > understand and I hope that somewhere here can explain it. > > Thank you. > -David McNeil > > -- > 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 > athttp://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: Defining a namespace inside a let
In a clean repl: C:\>repl Clojure 1.2.0-master-SNAPSHOT user=> (println (do (ns ns-1) (def my-namespace *ns*) my-namespace)) # nil ns-1=> On Apr 30, 1:17 am, alux wrote: > Hello Armando, did you try the second half of you experiment in a > clean REPL? > > As you describe it, the first evaluation may have created the var. > > Regards, alux > > On 29 Apr., 21:32, Armando Blancas wrote: > > > > > > > > The REPL switches to the namespace ns-1 and the var my-namespace is in > > > user ! > > > I don't see that with CLJ 1.2 on Windows: > > > user=> (do (ns ns-1) (def my-namespace *ns*) my-namespace) > > # > > ns-1=> (ns user) > > nil > > user=> (println (do (ns ns-1) (def my-namespace *ns*) my-namespace)) > > # > > nil > > ns-1=> (var my-namespace) > > #'ns-1/my-namespace > > > -- > > 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 > athttp://groups.google.com/group/clojure?hl=en- Hide quoted text - > > - Show quoted text - -- 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: Defining a namespace inside a let
Hello Armando, did you try the second half of you experiment in a clean REPL? As you describe it, the first evaluation may have created the var. Regards, alux On 29 Apr., 21:32, Armando Blancas wrote: > > The REPL switches to the namespace ns-1 and the var my-namespace is in > > user ! > > > I don't see that with CLJ 1.2 on Windows: > > > user=> (do (ns ns-1) (def my-namespace *ns*) my-namespace) > # > ns-1=> (ns user) > nil > user=> (println (do (ns ns-1) (def my-namespace *ns*) my-namespace)) > # > nil > ns-1=> (var my-namespace) > #'ns-1/my-namespace > > > -- > 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: Defining a namespace inside a let
> The REPL switches to the namespace ns-1 and the var my-namespace is in > user ! I don't see that with CLJ 1.2 on Windows: user=> (do (ns ns-1) (def my-namespace *ns*) my-namespace) # ns-1=> (ns user) nil user=> (println (do (ns ns-1) (def my-namespace *ns*) my-namespace)) # nil ns-1=> (var my-namespace) #'ns-1/my-namespace -- 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: Defining a namespace inside a let
Hello, interesting. Is this a bug?? Somehow the println seems to do strange things here. If I evaluate in namespace user (do (ns ns-1) (def my-namespace *ns*) my-namespace) The REPL switches to the namespace ns-1 and the var my-namespace is in ns-1 If I evaluate in namespace user (println (do (ns ns-1) (def my-namespace *ns*) my-namespace)) The REPL switches to the namespace ns-1 and the var my-namespace is in user ! That seems to be the background of Davids irritation. Any explanations? Kind regards, alux On 28 Apr., 17:37, Nate Young wrote: > On Apr 26, 5:25 pm, David McNeil wrote:> I am > experimenting with clojure.test and I encountered the following > > situation which I cannot explain. > > > This code: > > > (println (do > > (ns ns01 > > (:use clojure.test)) > > (deftest test1 nil) > > (run-tests))) > > > Produces the expected result (note: it runs one test): > > This is actually NOT what I'm seeing. At least when I run it, it > appears to run exactly zero tests. > > > [snip] > > > (println (let [] > > (do > > (ns ns02 > > (:use clojure.test)) > > (deftest test1 nil) > > (run-tests > > > Then I get the unexpected result that no tests are executed: > > The let here is, I believe a red herring. Again, with what I'm > seeing, this form executes the same way as the above and runs exactly > zero tests. > > > Seems there is something going on with namespaces that I do not > > understand and I hope that somewhere here can explain it. > > I think I can explain the above behavior and suggest a way to obtain > the behavior you want. > I don't think it has as much to do with namespaces as it does with the > evaluation rule and it's relationship to def. > > You've defined a do, which will execute each of the forms in turn, but > the deftest (a macro which expands to a def form) creates it's test > function in the namespace that the do was executed in, not the > namespace created and switched to just previously. > > In fact, if you check your namespace, you should see that test1 is > defined not in ns01/ns02, but in user (or whichever namespace you > executed the do from). > > What I can't sufficiently explain is why def doesn't see the binding > of *ns* but run-tests obviously does, as it tries to find all tests > defined in ns01/ns02 (of which there are none). > (println (do > (ns ns03 > (:use clojure.test)) > (binding [*ns* (find-ns 'ns03)] > (deftest test3 nil)) > (run-tests))) > > Now, when the do evaluates the second form, it uses the namespace that > was created by the first form. > > Hope this helps. > > -- > 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 > athttp://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: Defining a namespace inside a let
On Apr 26, 5:25 pm, David McNeil wrote: > I am experimenting with clojure.test and I encountered the following > situation which I cannot explain. > > This code: > > (println (do > (ns ns01 > (:use clojure.test)) > (deftest test1 nil) > (run-tests))) > > Produces the expected result (note: it runs one test): This is actually NOT what I'm seeing. At least when I run it, it appears to run exactly zero tests. > [snip] > > (println (let [] > (do > (ns ns02 > (:use clojure.test)) > (deftest test1 nil) > (run-tests > > Then I get the unexpected result that no tests are executed: The let here is, I believe a red herring. Again, with what I'm seeing, this form executes the same way as the above and runs exactly zero tests. > Seems there is something going on with namespaces that I do not > understand and I hope that somewhere here can explain it. I think I can explain the above behavior and suggest a way to obtain the behavior you want. I don't think it has as much to do with namespaces as it does with the evaluation rule and it's relationship to def. You've defined a do, which will execute each of the forms in turn, but the deftest (a macro which expands to a def form) creates it's test function in the namespace that the do was executed in, not the namespace created and switched to just previously. In fact, if you check your namespace, you should see that test1 is defined not in ns01/ns02, but in user (or whichever namespace you executed the do from). What I can't sufficiently explain is why def doesn't see the binding of *ns* but run-tests obviously does, as it tries to find all tests defined in ns01/ns02 (of which there are none). (println (do (ns ns03 (:use clojure.test)) (binding [*ns* (find-ns 'ns03)] (deftest test3 nil)) (run-tests))) Now, when the do evaluates the second form, it uses the namespace that was created by the first form. Hope this helps. -- 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: Defining a namespace inside a let
Not sure about your specific case, but when you don't get back expected results it's usually due to the functions called being lazy. Try doall or dorun to force the iterations. -Rgds, Adrian. On Tue, Apr 27, 2010 at 12:25 AM, David McNeil wrote: > I am experimenting with clojure.test and I encountered the following > situation which I cannot explain. > > This code: > > (println (do > (ns ns01 > (:use clojure.test)) > (deftest test1 nil) > (run-tests))) > > Produces the expected result (note: it runs one test): > > Testing ns01 > > Ran 1 tests containing 0 assertions. > 0 failures, 0 errors. > {:type :summary, :test 1, :pass 0, :fail 0, :error 0} > > However, if I do the exact same thing inside of a let: > > (println (let [] > (do > (ns ns02 > (:use clojure.test)) > (deftest test1 nil) > (run-tests > > Then I get the unexpected result that no tests are executed: > > Testing ns02 > > Ran 0 tests containing 0 assertions. > 0 failures, 0 errors. > {:type :summary, :test 0, :pass 0, :fail 0, :error 0} > > Seems there is something going on with namespaces that I do not > understand and I hope that somewhere here can explain it. > > Thank you. > -David McNeil > > -- > 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
Defining a namespace inside a let
I am experimenting with clojure.test and I encountered the following situation which I cannot explain. This code: (println (do (ns ns01 (:use clojure.test)) (deftest test1 nil) (run-tests))) Produces the expected result (note: it runs one test): Testing ns01 Ran 1 tests containing 0 assertions. 0 failures, 0 errors. {:type :summary, :test 1, :pass 0, :fail 0, :error 0} However, if I do the exact same thing inside of a let: (println (let [] (do (ns ns02 (:use clojure.test)) (deftest test1 nil) (run-tests Then I get the unexpected result that no tests are executed: Testing ns02 Ran 0 tests containing 0 assertions. 0 failures, 0 errors. {:type :summary, :test 0, :pass 0, :fail 0, :error 0} Seems there is something going on with namespaces that I do not understand and I hope that somewhere here can explain it. Thank you. -David McNeil -- 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