Skip to content

Instantly share code, notes, and snippets.

@youkale
Last active January 24, 2022 07:50
Show Gist options
  • Save youkale/1e35567863d2f8c0fbf09e3e37a97df2 to your computer and use it in GitHub Desktop.
Save youkale/1e35567863d2f8c0fbf09e3e37a97df2 to your computer and use it in GitHub Desktop.
Clojure魔改definline支持重载
(defmacro definline+
[name & decl]
(let [body (map (fn [[args# expr#]]
(let [f# (apply (eval (list `fn (list args# expr#))) args#)]
`(~args# ~f#))) decl)
alt-body (map (fn [[args# expr#]]
`(~args# ~expr#)) decl)]
`(do
(defn ~name ~@body)
(alter-meta! (var ~name) assoc :inline (fn ~name ~@alt-body))
(var ~name))))
@youkale
Copy link
Author

youkale commented Jan 24, 2022

;; 使用方法
(definline+ foo ([x] x)
([x y] (+ x y)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment