简体   繁体   English

我的Clojure代码是AOT编译的吗?

[英]Is my clojure code being AOT compiled?

I have a Maven project where I mix java and Clojure code with the help of the clojure-maven-plugin. 我有一个Maven项目,其中我在clojure-maven-plugin的帮助下混合了Java和Clojure代码。

I have a Clojure namespace like this: 我有一个Clojure命名空间,如下所示:

(ns org.myproject.clojure
    (:gen-class))

(defn do-stuff [] (println "Doing stuff"))

And from java, I call it like: 从Java,我这样称呼它:

IFn require = Clojure.var("clojure.core", "require");
require.invoke(Clojure.read("org.myproject.clojure"));
IFn do_stuff = Clojure.var("org.myproject.clojure", "do-stuff");
do_stuff.invoke();

When running this code, "Doing stuff" is correctly being printed to the screen. 运行此代码时,“正在做的事情”被正确打印到屏幕上。 Moreover, I can see .class files for my clojure code inside the generated jar from maven. 此外,我可以从maven生成的jar中看到我的clojure代码的.class文件。 Despite all that, I'm still not sure what's clojure doing when I require the namespace. 尽管如此,当我需要命名空间时,我仍然不确定clojure在做什么。 Is it compiling the whole thing all over again? 它会重新编译整个事情吗? Or is my code being properly AOT compiled with my setup? 还是我的代码已通过我的设置正确地AOT编译?

Finally, is there a way I can treat my clojure code as "regular" java functions? 最后,有什么方法可以将clojure代码视为“常规” java函数? That is, importing org.myproject.clojure and calling do-stuff as a regular function? 也就是说,导入org.myproject.clojure并将do-stuff作为常规函数调用? Given that clojure is generating bytecode for my code. 鉴于clojure正在为我的代码生成字节码。 I don't see why java couldn't call it being bytecode compatible. 我不明白为什么Java不能称其为字节码兼容。

Require does not compile your code, it loads the necessary classes dynamically. Require不会编译您的代码,它会动态加载必要的类。 Your code is already compiled, unless of course you call compile yourself on another file. 您的代码已被编译,除非您当然要在另一个文件上进行compile

Second, you cannot use Clojure functions as Java functions, because they are not the same thing. 其次,您不能将Clojure函数用作Java函数,因为它们不是同一回事。 As you can see, Clojure functions are objects that implements IFn and have invoke methods. 如您所见,Clojure函数是实现IFn并具有invoke方法的对象。 I don't think using directly Java methods would be feasible: you have to call methods on classes or instances, but which objects owns the methods? 我认为直接使用Java方法并不可行:您必须在类或实例上调用方法,但是哪个对象拥有方法? Further, using objects to implement functions means that they are truly first-class and that you can easily change bindings at runtime, which is what dynamicity is about. 此外,使用对象来实现功能意味着它们确实是一流的,并且您可以在运行时轻松更改绑定,这就是动态性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM