Fork me on GitHub
Piotr Kaznowski08:11:44

Hi all! Is it possible in bb tasks to evaluate a symbol? Like instead of:

# bb.edn
to have something like:
 foo-bar (symbol "" "-main")}
It's not returning errors, but it doesn't seem to do anything either (like it's rather returning the symbol than evaluating it). Using resolve returns nil.


Hi @UUAKPEMJ9! Is your goal to execute a main function dynamically?


resolve returns nil because you should in this case load the namespace yourself

Piotr Kaznowski08:11:36

Hmm, I'm still learning clj, so I may be confusing something. My goal is actually to create the symbol to be evaluated from the command line args (context: writing a setup for Advent of Code in bb, and want to have task to run solution for a puzzle of a given day, like bb solve 1. I'd have a namespace day1.solution with a -main func there. But each day will add another namespace, and I don't want to update tasks each day bc I want to automate stuff 🙂).


You could use requiring-resolve:

(requiring-resolve (symbol "" "-main"))


that gets you the var, but then you still need to call that a function:

(apply (requiring-resolve (symbol "" "-main")) *command-line-args*)


or just

((requiring-resolve (symbol "" "-main")))
if you don't want to feed it any command line args

Piotr Kaznowski08:11:58

Excellent -- that works! Thank you 🙂


You can also try:

{:task (exec (symbol "" "baz"))}

👏 1

What this does is it parses the command line arguments with babashka.cli and then executes the function denoted by the symbol with those arguments

Piotr Kaznowski08:11:39

And that works, too