Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
def: refine defalias further, deals now with only root binding, clean…
Browse files Browse the repository at this point in the history
…ly merges metadata and provides doc string, conforms to DRY principle
  • Loading branch information
scgilardi committed Jan 31, 2009
1 parent eb489c7 commit cbbf5c1
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/clojure/contrib/def.clj
Expand Up @@ -60,17 +60,20 @@
(list `defonce (with-meta name (assoc (meta name) :private true :doc doc)) expr)))

(defmacro defalias
"Defines an alias for a var: a new var with the same value and metadata
as another with the exception of :namespace, :name, :file, :line, and
optionally :doc which are those of new var."
"Defines an alias for a var: a new var with the same root binding (if
any) and similar metadata. The metadata of the alias is its initial
metadata (as provided by def) merged into the metadata of the original."
([name orig]
`(let [o# #'~orig v# (def ~name @o#)]
(alter-meta! v# merge ^o# ^v#)
v#))
`(do
(alter-meta!
(if (.hasRoot (var ~orig))
(def ~name (.getRoot (var ~orig)))
(def ~name))
conj
(apply dissoc (meta (var ~orig)) (keys (meta (var ~name)))))
(var ~name)))
([name orig doc]
`(let [o# #'~orig v# (def ~name @o#)]
(alter-meta! v# merge ^o# ^v# {:doc ~doc})
v#)))
(list `defalias (with-meta name (assoc (meta name) :doc doc)) orig)))

; defhinted by Chouser:
(defmacro defhinted
Expand Down

0 comments on commit cbbf5c1

Please sign in to comment.