Reputation: 9049
I have a function which loads expressions from another file, but I don't know what the filename is, it is stored in a variable:
(defn run-migration [filename]
(load filename)
(run))
I know that all of these files have a common method called "run". So I try to call it after loading in that function, but I get the "Unable to resolve symbol: run" error when I try to require this file in the repl, before the file is even loaded. Apparently clojure is trying to compile the file and "run" is not bound at that time because the load happens inside a function?
Possibly I am going about this the wrong way. Any guidance on a good (idiomatic) way to have a set of files that get loaded and run at runtime?
Upvotes: 4
Views: 555
Reputation: 87119
In one of my projects, I dynamically load modules using following code (snippet of real code):
... loop over found namespaces with following body....
(require (vector n :reload true))
(let [load-fun (ns-resolve n (symbol "load-rules"))]
(when load-fun
(try
(load-fun)
(catch Exception ex
(error (str "Error during executing of func from namespace '" n "': " ex))))))
here n
is symbol, representing namespace. This symbol is constructed dynamically by searching in classpath... Here is example of code that I use to find modules in classpath
Upvotes: 3
Reputation: 91534
you can tell the compiler that the function run
will defined later:
user> (declare run)
#'user/run
user> (load "filename")
that will get your file loaded into the repl. Perhaps you may want to set the namespace you load the file into by binding ns though this may not be necessarily.
Upvotes: 2