AlMcLean
AlMcLean

Reputation: 3559

Clojure Newbie - Namespace troubles

I'm learning Clojure and so far I can't make sense of this little conundrum which I'm sure is ridiculously basic.

I have this file :

(ns cloapp.core
  (:gen-class))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "Hello, World!")
  (println "Well Hi there, im a string !")
  (println "Why wont this work !")
  (myFunc "Hiya"))

(defn myFunc [aVar]
    (println aVar))

If I try and run this with,

lein run

It complains and says,

Caused by: java.lang.RuntimeException: Unable to resolve symbol: myFunc in this context

But if I remove the call to myFunc from main and do,

lein repl
cloapp.core=> (myFunc "Hiya !")
Hiya !
nil
cloapp.core=> 

Then I can call it. Why is this ? I'm assuming it's something to do with the namespace but reading up on it I can't work it out.

A

Upvotes: 4

Views: 146

Answers (1)

Kevin
Kevin

Reputation: 25269

The myFunc symbol has not been defined yet, so main can't find it. If you move the definition of myFunc above main then it will work.

Upvotes: 7

Related Questions