graalvm 2024-07-21

Alex Miller (Clojure team) 2024-07-21T13:19:35.224679Z

It will use the function return type hint if it exists, otherwise assumes Object

Yes, how to get that information using some code?

function's metadata can be obtained with meta, but I think Clojure implements type inference

You can set to warn on reflection, (set! *warn-on-reflection* true). Usually, I just turn that on and fix the warnings as necessary. This talk gives a good overview, https://www.youtube.com/watch?v=s_xjnXB994w.

*warn-on-reflection* was told to me before, but I see a potential problem with all functions that fail type inference and am trying to display this in the editor. I would like to get this information from Clojure.

> but I see a potential problem with all functions that fail type inference Can you give a few more details about the problem?

What goal are you trying to achieve where using *warn-on-reflection* falls short?

That option will issue a warning at runtime, but this type of warning should be able to be issued statically.

You can get the warnings to show just by setting warn on reflection and loading the namespace. Do have a repl connected during development?

Especially for graalvm, which requires AOT compilation, you should see the warnings during compilation.

Some libraries set warn on reflection at the top of the namespace since they're intended to support native image compilation. For example, https://github.com/cnuernber/dtype-next/blob/a9d05cd39905394c7352496ea10de65e3e3475dd/src/tech/v3/datatype/wavelet.clj#L11.

You're right, sorry. No, I still think the main reason is that I think there is a potential problem. I will be able to write type hints on the fly when I don't guess the type I think I have at the moment I write the function.

It warn β€œwhen reflection occurs,” but I want to warn β€œcode where reflection can occur.”

It warns on compilation.

user> (set! *warn-on-reflection* true)
user> (defn my-ends-with [s]
        (.endsWith s "laksdf"))
Reflection warning - call to method endsWith can't be resolved (target class is unknown).
#'user/my-ends-with

Yes, it is. But what if the call that causes the reflection is in a different namespace? It's not practical to evaluate all namespaces every time a function is changed.

Assumes that a function is defined in one namespace and called in another namespace.

I don't know of an alternative, but I've never found that to be a problem in practice.

Is this a problem you've run into or is this a theoretical problem?

You can set warn-on-reflection globally so that when you load a namespace or AOT compile it will also show warnings there too.

Theoretical. this time the problem is solved. I just want to receive warnings when I write a function, even when there is no call to it.

my-ends-with produces a reflection warning even though it is never called

https://github.com/conao3/clojure-graalvm-list-issue/blob/master/src/graalvm_list_issue/main.clj No. In this example, I want to start writing the source code from the top and notice that when I write local-dir, the type information is missing. I should be able to receive a warning without a call to .list with -main.

Maybe Clojure doesn't perform this kind of type inference in the first place, but I'd rather even implement it.

local-dir does not produce any reflection warnings

when you call .list, that is what produces the reflection warning

so when you write main, you can decide if you want to add type hints to local-dir or you can add type hints inline:

(defn -main [& _args]
  (println (seq (.list ^File (io/file (home-path) ".local"))))
  (println (seq (.list ^File (local-dir)))))

Sorry, I don't like to write type hints in the caller, it's not DRY, the problem is that the return type of local-dir is not guessed and I want to be aware of that fact.

And I think this type of type inference should be implemented in Clojure

Sounds good. I've written quite a bit of code that compiles to native image and I haven't really found it to be a problem. If you want to make a case for this type of inference, you can check if there's already an issue on https://ask.clojure.org/ and upvote or create a new question.

πŸ‘ 1

fwiw, many common java utilities already have wrappers that avoid reflection. Specifically for file system utilities, you can try https://github.com/babashka/fs?tab=readme-ov-file

OK! thanks! I'm a Clojure beginner so I'll write more code and get more experience.

1
πŸŽ‰ 1

In relation to the previous question, is it possible to investigate how Clojure infers the return value of a function?