hunajakettu

joined 1 year ago
[โ€“] hunajakettu@alien.top 1 points 1 year ago

It is realy intresting, thanks!

[โ€“] hunajakettu@alien.top 1 points 1 year ago

Could you point to a similar code (easy to understand) written using the expressivity of Clojure?

I want to learn better Clojure

[โ€“] hunajakettu@alien.top 1 points 1 year ago

Oh, yes, well this is a code hack to make it as small as possible, but still having the same issue, without thinking much, thanks anyway!

[โ€“] hunajakettu@alien.top 1 points 1 year ago (2 children)

Another question, I'm experimenting a bit, and if I use ^long further up (for example in the let declaration[sleep-time (* ^long (handle2 max-time) 1000)]) it also seems to work.

Does it matter where it goes?

[โ€“] hunajakettu@alien.top 1 points 1 year ago

Ok, so reflection is something that is to be avoided in graalVM ? Or in general with clojure?
Reading [this](https://clojure.org/reference/java_interop#typehints) I get the impression that it should be used for performance, why would it affect the compiled object, if I don't care about performance?

(Sorry if it seems that I'm obtuse.)

[โ€“] hunajakettu@alien.top 1 points 1 year ago

Ok, this worked, but why? Somewhere to read about this?

 

I have the following code (reduced to a minimum working example):

(ns timer.core (:gen-class))

(defn handle2 [x]
  (if (string? x) (Integer/parseInt x) x))

(defn -main
  ([max-time]
   (println (format "Going to sleep %d seconds" (handle2 max-time)))  
   (let [sleep-time (* (handle2 max-time) 1000)]  
     (Thread/sleep sleep-time)  
     (println (format "Waken up"))))  
  ([] (-main 10)))  

that works in the cider REPl on emacs and also works with > lein run on a windows terminal (x64 Native Command Prompt for VS 2022) and also can be compiled into an uberjar with leiningen and calling the java -jar command works. All the previous work with or without the max-time argument, as it is expected.

But when I build a executable from the uberjar with the GraalVM using the command

>  native-image --report-unsupported-elements-at-runtime --initialize-at-build-time -jar .\target\timer-0.1.0-SNAPSHOT-standalone.jar -H:Name=timer-cli

although it compiles a timer-cli.exe executable binary, this fails when is run with this error:

exp-timer-cli.exe
Going to sleep 10 seconds
Exception in thread "main" java.lang.IllegalArgumentException: No matching method sleep found taking 1 args
        at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:127)
        at clojure.lang.Reflector.invokeStaticMethod(Reflector.java:332)
        at timer.core$_main.invokeStatic(core.clj:72)
        at timer.core$_main.invoke(core.clj:68)
        at timer.core$_main.invokeStatic(core.clj:76)
        at timer.core$_main.invoke(core.clj:68)
        at clojure.lang.AFn.applyToHelper(AFn.java:152)
        at clojure.lang.AFn.applyTo(AFn.java:144)
        at timer.core.main(Unknown Source)

with or without parameter. But if I substitute (handle2 max-time) by an actual number (let's say 2), the program works, but of course with the hardcoded variable as sleep time, and prints the parameter value on the message string.

Does any one have experience with the GraalVM and knows why this happens? And how to solve this would be greatly appreciated.

Using:

  • Leiningen 2.10.0 on Java 19.0.2 OpenJDK 64-Bit Server VM
  • GraalVM 22.3.1