(import 'java.io.DataOutputStream) (import 'java.io.ByteArrayOutputStream) (defn- ->bytes "Convert a Java primitive to its byte representation." [write v] (let [output-stream (ByteArrayOutputStream.) data-output (DataOutputStream. output-stream)] (write data-output v) (seq (.toByteArray output-stream))))
(defn int->bytes [n] (->bytes #(.writeInt ^DataOutputStream %1 %2) n)) (defn int->bytes-ref [n] (->bytes #(.writeInt %1 %2) n)) user=> (int->bytes 5) (0 0 0 5) user=> (int->bytes-ref 5) (0 0 0 5) user=> (int->bytes (inc Integer/MAX_VALUE)) IllegalArgumentException Value out of range for int: 2147483648 clojure.lang.RT.intCast (RT.java:1115) user=> (int->bytes-ref (inc Integer/MAX_VALUE)) (-128 0 0 0) So it looks like type-hinting the DataOutputStream results in bytecode calling RT.intCast, which throws because the value is too large. In the reflective case, we locate the method writeInt at runtime, and then do not call RT.intCast, but instead allow the long to be downcast without bounds checking. It seems like we should be calling RT.intCast in both cases? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.