Fork me on GitHub
#java
<
2022-12-15
>
jumar11:12:54

When you need to call a Clojure function from Java, is this the easiest way to do it?

IFn require = Clojure.var("clojure.core", "require");
        require.invoke(clojure.lang.Symbol.create("stats-clj.special"));
        IFn inc = Clojure.var("stats-clj.special", "gamma");
        System.out.println(inc.invoke(5));
I found part of it here https://clojure.org/reference/java_interop#_calling_clojure_from_java But it's not talking about how to actually load custom code.

Alex Miller (Clojure team)13:12:19

This is the correct way to do it. What does ‘custom code” mean?

Alex Miller (Clojure team)13:12:10

With above you can load any namespace and invoke any var

jumar13:12:07

By "custom code" I meant simply loading something outside clojure.core, like the stats-clj.special ns above 🙂 Thanks for confirmation - perhaps it would be a good addition to the .org reference linked above?

Alex Miller (Clojure team)14:12:59

I don’t understand what’s different that needs to be added

Alex Miller (Clojure team)14:12:41

It says “Functions in clojure.core are automatically loaded. Other namespaces can be loaded via require:”

jumar06:12:12

Oh I completely missed that example - sorry, all is good then

emccue14:12:15

technically clojure.lang.Symbol.create is not part of the api

emccue14:12:24

its just var and IFn

Alex Miller (Clojure team)15:12:10

You can use Clojure.read() instead