Ah of course. Thanks a lot, your explanation makes things a lot
clearer! I guess because of the direct nature of the REPL I hadn't
thought about compile time vs runtime, but that makes perfect sense.



On Dec 29, 8:00 pm, Meikel Brandmeyer <m...@kotka.de> wrote:
> Hi Kees-Jochem,
>
> Am 29.12.2008 um 20:37 schrieb Kees-Jochem Wehrmeijer:
>
> > I created a small file foo.clj with the following contents:
> > (defn foo [] :bar)
>
> > Then from a REPL I try the following:
> > (do (load-file "foo.clj") (foo))
>
> > but this gives an error message:
> > java.lang.Exception: Unable to resolve symbol: foo in this context
> > (NO_SOURCE_FILE :1)
>
> You have to distinguish compile time from runtime.
> The load-file happens during runtime. However the
> compiler looks about foo already during compile
> time. But since the file is not loaded, the compiler
> doesn't find foo and, hence, complains.
>
> > Strangely enough, the following does work:
> > (do (load-file "foo.clj") (ns-interns 'user))
> > This returns {foo #'user/foo} (as expected)
>
> Not at all strange: Since the *call* to ns-interns
> happens at runtime, the foo.clj is already loaded.
> Hence, foo is defined and everything is fine.
>
> > So, while it seems that the file gets loaded and
> > the function gets added to the namespace, calling
> > it from within the do doesn't seem to work. What's
> > going on?
>
> So it's not actually the do, but the different times
> when things happen.
>
> Instead of load-file, use a namespace. Although,
> this may already be a bit involved depending how far
> you already got with your learning.
>
> Create a file named foo/bar.clj in your classpath.
> In this file put the following:
>
> (ns foo.bar)
>
> (defn foo [] :bar)
>
> Then do at the Repl:
>
> (use 'foo.bar)
> (foo)
>
> This should work without problems, since use loads
> the file at compile time. So the compiler knows
> about the foo definition and everything works out.
>
> Hope this helps.
>
> Sincerely
> Meikel
>
>  smime.p7s
> 5KViewDownload
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to