How can I reference functions from the downloaded file?

I have a function that loads expressions from another file, but I don’t know what the file name 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". Therefore, I try to call it after loading into this function, but when I try to request this file in repl, I get the error "Unable to resolve the error: execute" before the file is loaded. Apparently, clojure is trying to compile the file, and "run" is not connected at the time, because the download is happening inside the function?

Maybe I'm wrong. Any guidance on a good (idiomatic) way to have a set of files that are loaded and executed at runtime?
+4
source share
2 answers

you can tell the compiler that the run function will be defined later:

 user> (declare run) #'user/run user> (load "filename") 

which will upload your file to repl. You might want to set the namespace to which you upload the file by binding ns, although this may not be necessary.

+2
source

In one of my projects, I dynamically load modules using the following code (a 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 a character representing a namespace. This symbol is built dynamically by searching in the class path ... Here is an example of the code that I use to search for modules in the class path

+3
source

All Articles