Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

def: refine defalias further, deals now with only root binding, clean…

…ly merges metadata and provides doc string, conforms to DRY principle
  • Loading branch information...
commit cbbf5c120cb13bfcf1dd78c74f270589b78aed28 1 parent eb489c7
Stephen C. Gilardi authored

Showing 1 changed file with 12 additions and 9 deletions. Show diff stats Hide diff stats

  1. 21  src/clojure/contrib/def.clj
21  src/clojure/contrib/def.clj
@@ -60,17 +60,20 @@
60 60
      (list `defonce (with-meta name (assoc (meta name) :private true :doc doc)) expr)))
61 61
 
62 62
 (defmacro defalias
63  
-  "Defines an alias for a var: a new var with the same value and metadata
64  
-  as another with the exception of :namespace, :name, :file, :line, and
65  
-  optionally :doc which are those of new var."
  63
+  "Defines an alias for a var: a new var with the same root binding (if
  64
+  any) and similar metadata. The metadata of the alias is its initial
  65
+  metadata (as provided by def) merged into the metadata of the original."
66 66
   ([name orig]
67  
-     `(let [o# #'~orig v# (def ~name @o#)]
68  
-        (alter-meta! v# merge ^o# ^v#)
69  
-        v#))
  67
+     `(do
  68
+        (alter-meta!
  69
+         (if (.hasRoot (var ~orig))
  70
+           (def ~name (.getRoot (var ~orig)))
  71
+           (def ~name))
  72
+         conj
  73
+         (apply dissoc (meta (var ~orig)) (keys (meta (var ~name)))))
  74
+        (var ~name)))
70 75
   ([name orig doc]
71  
-     `(let [o# #'~orig v# (def ~name @o#)]
72  
-        (alter-meta! v# merge ^o# ^v# {:doc ~doc})
73  
-        v#)))
  76
+     (list `defalias (with-meta name (assoc (meta name) :doc doc)) orig)))
74 77
 
75 78
 ; defhinted by Chouser:
76 79
 (defmacro defhinted

0 notes on commit cbbf5c1

Please sign in to comment.
Something went wrong with that request. Please try again.