diff options
author | scgilardi <scgilardi@gmail.com> | 2009-01-31 01:23:09 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2009-01-31 01:23:09 +0000 |
commit | cbbf5c120cb13bfcf1dd78c74f270589b78aed28 (patch) | |
tree | 14fbe768f6cd559764dc84f573c9d444f534b447 | |
parent | eb489c7ddac4fb2a4b6dcd60ccdb9b7440a3f8b9 (diff) |
def: refine defalias further, deals now with only root binding, cleanly merges metadata and provides doc string, conforms to DRY principle
-rw-r--r-- | src/clojure/contrib/def.clj | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/clojure/contrib/def.clj b/src/clojure/contrib/def.clj index 2debdf88..aff4a9d3 100644 --- a/src/clojure/contrib/def.clj +++ b/src/clojure/contrib/def.clj @@ -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 |