Welcome! Please see the About page for a little more info on how this works.

+2 votes
in Clojure by
edited by

I'm essentially looking for the Clojure version of Java's import static. I understand that this is usually bad practice, but I'm working with LWJGL which has a lot of static methods like GLFW/glfwSetWindowPos and GLFW/glfwMakeContextCurrent, where the namespacing is part of the method name (due to being a thin wrapper around a C library). I find the repetition adds a lot of visual noise that makes the code harder to read, especially when doubly-namespaced functions take doubly-namespaced arguments like in

(GL15/glCreateShader GL15/GL_VERTEX_SHADER)

I found https://clojure.github.io/clojure-contrib/import-static-api.html, but seeing as clojure.contrib is deprecated, I'd prefer not to depend on it.

2 Answers

+2 votes

No, there is no equivalent. The link you have there is the best impl I’m aware of that’s like this. The clojure-contrib lib is deprecated and quite old now so I’m afraid I wouldn’t recommend using it.

That's roughly what I was worried about. Ah well, thanks anyway.
+1 vote

The source from the link is pretty small and only depends on clojure.set. So...I would just copy the ns or the macro (with attribution from the license at the top of the namespace) and see if it works for your use case (probably, doesn't seem too exotic).

The other option would be to just wrap all the static class methods you are using a lot in functions. I could envision macros that do this as well; you probably want to preserve the type information and the like.

At a cursory look, this is what the contrib tool does.