Created
March 27, 2017 20:45
-
-
Save noisesmith/d0ee8172e1e24f574ffa760c4dc30be2 to your computer and use it in GitHub Desktop.
a macro to make varargs interop more readable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=> (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