Given any compiled program in Java, is it possible to write an equivalent version of bytecode in Clojure?

Clojure runs on the JVM and theoretically can do everything Java can do due to Java interaction, but is it really at the bytecode level? I am responding to bytecode equivalence to emphasize performance, I suppose you might have to skip writing idiomatic code. If possible, does this require the use of a bytecode generation library?

To be more specific, I'm interested in the existence of odd corner cases where Java provides constructs that build bytecode in a completely different way. I'm not interested in purely linguistic expressiveness, but rather the progress of clojure's ability to write clojure in clojure or any new primitive.

+4
source share
4 answers

No. To give an example, I don’t think there is a way to create an if in clojure that generates identical bytecode for what is in a Java program.

+3
source
Programs

Clojure will always have extra baggage during operation, which may make them unsuitable for situations with extremely low levels of resources (for example, Android). But if you don't mind a few extra megabytes to drive, you can always achieve Java-level performance by throwing the agreement out of the window.

Alex Osborne has a great example of optimizing a Clojure program until it infers a rival to Java: http://meshy.org/2009/12/13/widefinder-2-with-clojure.html (of course, the same level of attention could speed up Java version.)

+3
source

The goal of the nearly full version 1.3 is to get the important part (main libraries and collections) in order to be able to create equivalent bytecode classes. this is part of the upcoming clojure-in-clojure goal.

In the end, Clojure has to create equivalent but not identical bytecode for everything that has an equivalent in java. Clojure may represent things (such as closure) that java is not, of course, they will not be "equivalent."

+3
source

No: Java programs created in Clojure will be slightly larger (in terms of the final size of the bytecode), which makes it impossible to create files with the same bytecode. Not that it really mattered.

In terms of functionality, Clojure's java interaction is very good, and so you can create functionally equivalent code compared to any Java code with asymptotically equivalent performance characteristics. (There may be some extremely, extremely obscure Java functions that are not available in Clojure, but I believe that java interaction in Clojure has almost 100% coverage at the moment.)

+1
source

Source: https://habr.com/ru/post/1341673/


All Articles