Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Created March 27, 2017 20:45
Show Gist options
  • Save noisesmith/d0ee8172e1e24f574ffa760c4dc30be2 to your computer and use it in GitHub Desktop.
Save noisesmith/d0ee8172e1e24f574ffa760c4dc30be2 to your computer and use it in GitHub Desktop.
a macro to make varargs interop more readable
=> (defmacro vararg
[method object & args]
(let [[regular [_ t & variable]] (split-with #(not= % '|) args)]
`(. ~object ~method ~@regular (into-array ~t ~(vec variable)))))
#'user/vararg
=> (macroexpand-1 '(vararg get java.nio.file.Paths "foo" | String "bar" "baz"))
(. java.nio.file.Paths get "foo" (clojure.core/into-array String ["bar" "baz"]))
=> (vararg get java.nio.file.Paths "foo" | String "bar" "baz")
#object[sun.nio.fs.UnixPath 0x14ebb640 "foo/bar/baz"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment