简体   繁体   中英

Clojure java interop - calling overloaded (static) methods

Although I do somewhat understand the reasons behind it, I find LWJGL's splitting of OpenGL methods and values across numerous classes rather annoying. Since I prefer dealing with functions anyway, I'm currently writing a Clojure module that exposes the OpenGL 3.3 core functions and constants in a single namespace.

Now the problem is, some OpenGL functions have overloads in LWJGL like for instance glBindAttribLocation has one for ByteBuffer and one for CharSequence . Now I'm saying 'problem' in the broadest sense because I'm not sure yet whether this really is one.

Can I for instance just write

(defn glBindAttribLocation [program index name]
  (GL20/glBindAttribLocation program index name))

and trust that Clojure will figure out which overload to call, or do I have to do this manually with some typehint-uglyness?

On the same note, many of LWJGL's functions take float s or int s - but I hear Clojure itself only uses long and double (of the primitives that is). Does that mean that everytime I call one of these functions, whatever I have gets converted to an Integer ( Float ) and then from there to an int ( float ) whenever I call one of these functions?

Yes, you will likely need to coerce data to type proper type if there are multiple candidate methods for an invocation due to overloading. The good news is that the Clojure compiler will likely raise an error, rather than give you surprising behavior.

See: Clojure overloaded method resolution for Longs

I've had weird problems with long->int conversion, but only when there is an overloaded signature for both int and long , creating ambiguities. If there are only methods for int and float (which, from looking at the documentation seems to be the case) then you should be fine. Clojure will automatically do the long->int and double->float conversions:

user=> (Float/isInfinite 5.0) ; Takes a float
false
user=> (Integer/numberOfTrailingZeros 4) ; Takes an int
2

As for GL20/glBindAttribLocation , the only times I've had to cast for overloaded methods are 1) if they have both an int and long version or 2) if I'm passing nil as a parameter for an object, making it impossible to guess which method I wanted. Since the first two arguments must be int s, unless you can pass nil for the name parameter then you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM