aboutsummaryrefslogtreecommitdiff
path: root/src/clojure
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2008-08-16 22:51:12 +0000
committerscgilardi <scgilardi@gmail.com>2008-08-16 22:51:12 +0000
commit262581797f02ee4dcf04cff90beaa0a13b176a8b (patch)
treeeada2f3cb99af8d03e86dbe09250ecf8c46fe758 /src/clojure
parent3dcb49711054c8f63313a6296cc23404d9e27294 (diff)
move def to src/clojure/contrib (overlooked in 132
Diffstat (limited to 'src/clojure')
-rw-r--r--src/clojure/contrib/def/def.clj74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/clojure/contrib/def/def.clj b/src/clojure/contrib/def/def.clj
new file mode 100644
index 00000000..ee7b3386
--- /dev/null
+++ b/src/clojure/contrib/def/def.clj
@@ -0,0 +1,74 @@
+;; Copyright (c) Stephen C. Gilardi. All rights reserved.
+;; The use and distribution terms for this software are covered by the
+;; Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+;; which can be found in the file CPL.TXT at the root of this distribution.
+;; By using this software in any fashion, you are agreeing to be bound by
+;; the terms of this license.
+;; You must not remove this notice, or any other, from this software.
+;;
+;; File: def.clj
+;;
+;; def.clj provides variants of def that make including doc strings and
+;; making private definitions more succinct.
+;;
+;; scgilardi (gmail)
+;; 17 May 2008
+
+(clojure/in-ns 'clojure.contrib.def)
+(clojure/refer 'clojure)
+
+(defmacro init-once
+ "Initializes a var exactly once. The var must already exist."
+ [var init]
+ `(let [v# (resolve '~var)]
+ (when-not (.isBound v#)
+ (.bindRoot v# ~init))))
+
+(defmacro defvar
+ "Defines a var with an optional intializer and doc string"
+ ([name]
+ (list `def name))
+ ([name init]
+ (list `def name init))
+ ([name init doc]
+ (list `def (with-meta name (assoc (meta name) :doc doc)) init)))
+
+(defmacro defunbound
+ "Defines an unbound var with optional doc string"
+ ([name]
+ (list `def name))
+ ([name doc]
+ (list `def (with-meta name (assoc (meta name) :doc doc)))))
+
+(defmacro defmacro-
+ "Same as defmacro but yields a private definition"
+ [name & decls]
+ (list* `defmacro (with-meta name (assoc (meta name) :private true)) decls))
+
+(defmacro defvar-
+ "Same as defvar but yields a private definition"
+ [name & decls]
+ (list* `defvar (with-meta name (assoc (meta name) :private true)) decls))
+
+(defmacro defunbound-
+ "Same as defunbound but yields a private definition"
+ [name & decls]
+ (list* `defunbound (with-meta name (assoc (meta name) :private true)) decls))
+
+(defmacro defstruct-
+ "Same as defstruct but yields a private definition"
+ [name & decls]
+ (list* `defstruct (with-meta name (assoc (meta name) :private true)) decls))
+
+(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."
+ ([name orig]
+ `(let [v# (def ~name ~orig)]
+ (. v# (setMeta (merge (meta #'~orig) (meta #'~name))))
+ v#))
+ ([name orig doc]
+ `(let [v# (def ~name ~orig)]
+ (. v# (setMeta (merge (meta #'~orig) (assoc (meta #'~name) :doc ~doc))))
+ v#)))