graalvm

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

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

conao3 2024-07-22T00:09:36.773469Z

Yes, how to get that information using some code?

conao3 2024-07-22T00:10:12.345829Z

https://docs.python.org/ja/3/library/typing.html#typing.get_type_hints Python has a typing.get_type_hint()

conao3 2024-07-22T00:12:43.940529Z

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

phronmophobic 2024-07-22T00:16:18.325179Z

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.

conao3 2024-07-22T00:18:55.820079Z

*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.

phronmophobic 2024-07-22T00:21:20.849679Z

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

phronmophobic 2024-07-22T00:22:53.723709Z

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

conao3 2024-07-22T00:23:11.527019Z

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

phronmophobic 2024-07-22T00:24:34.930269Z

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

phronmophobic 2024-07-22T00:25:29.119859Z

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

phronmophobic 2024-07-22T00:26:31.419659Z

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.

conao3 2024-07-22T00:26:37.845789Z

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.

conao3 2024-07-22T00:27:48.289609Z

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

phronmophobic 2024-07-22T00:28:18.605609Z

It warns on compilation.

phronmophobic 2024-07-22T00:29:43.757609Z

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

conao3 2024-07-22T00:29:46.757689Z

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.

conao3 2024-07-22T00:30:41.513779Z

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

phronmophobic 2024-07-22T00:34:08.094219Z

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

conao3 2024-07-22T00:34:42.625679Z

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L361 My guess is that this function in Clojure has type information.

phronmophobic 2024-07-22T00:36:58.867629Z

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

phronmophobic 2024-07-22T00:37:45.625989Z

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

conao3 2024-07-22T00:39:22.766419Z

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.

phronmophobic 2024-07-22T00:40:07.337829Z

Is that not what this example is showing? https://clojurians.slack.com/archives/CAJN79WNT/p1721608183757609?thread_ts=1721567975.224679&cid=CAJN79WNT

phronmophobic 2024-07-22T00:40:24.891639Z

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

conao3 2024-07-22T00:42:01.258229Z

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.

conao3 2024-07-22T00:42:50.681909Z

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

phronmophobic 2024-07-22T00:43:06.451459Z

local-dir does not produce any reflection warnings

phronmophobic 2024-07-22T00:44:15.559309Z

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

phronmophobic 2024-07-22T00:46:41.033269Z

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)))))

conao3 2024-07-22T00:49:25.392019Z

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.

conao3 2024-07-22T00:50:14.263239Z

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

phronmophobic 2024-07-22T00:52:30.603289Z

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
phronmophobic 2024-07-22T00:53:39.955219Z

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

conao3 2024-07-22T00:55:13.867309Z

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

1
πŸŽ‰ 1
conao3 2024-07-22T03:37:29.590569Z

FYI: Submitted: https://ask.clojure.org/index.php/14027/type-inference-in-defn

πŸ‘ 1
conao3 2024-07-21T05:11:42.274269Z

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