diff options
author | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-03-12 13:33:25 +0000 |
---|---|---|
committer | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-03-12 13:33:25 +0000 |
commit | ea60eb05432f24f7244eb36a7f952236a04ebbb1 (patch) | |
tree | eb745fda26463f6a321e9ada0d9f5d36b0486526 /src/clojure/contrib | |
parent | 541a4bcfdbfe21f7a61a523a69a7dd4a91f7801b (diff) |
types: private type definitions
Diffstat (limited to 'src/clojure/contrib')
-rw-r--r-- | src/clojure/contrib/stream_utils.clj | 10 | ||||
-rw-r--r-- | src/clojure/contrib/types.clj | 29 |
2 files changed, 25 insertions, 14 deletions
diff --git a/src/clojure/contrib/stream_utils.clj b/src/clojure/contrib/stream_utils.clj index 75b8568d..7ce9933f 100644 --- a/src/clojure/contrib/stream_utils.clj +++ b/src/clojure/contrib/stream_utils.clj @@ -1,7 +1,7 @@ ;; Stream utilities ;; by Konrad Hinsen -;; last updated March 11, 2009 +;; last updated March 12, 2009 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use ;; and distribution terms for this software are covered by the Eclipse @@ -53,7 +53,7 @@ the next value of stream argument s, whereas pick-all returns the next value of all stream arguments in the form of a vector." - (:use [clojure.contrib.types :only (deftype)]) + (:use [clojure.contrib.types :only (deftype deftype-)]) (:use [clojure.contrib.monads :only (defmonad with-monad)]) (:use [clojure.contrib.def :only (defvar defvar-)]) (:require [clojure.contrib.seq-utils])) @@ -198,7 +198,7 @@ (recur (dec n) s)))) ; Map a function on a stream -(deftype ::stream-map stream-map-state) +(deftype- ::stream-map stream-map-state) (defstream ::stream-map [[f stream]] @@ -221,7 +221,7 @@ (stream-map-state [(comp f g) stream])) ; Filter stream elements -(deftype ::stream-filter stream-filter-state) +(deftype- ::stream-filter stream-filter-state) (defstream ::stream-filter [[p stream]] @@ -246,7 +246,7 @@ (stream-filter-state [(fn [v] (and (q v) (p v))) stream])) ; Flatten a stream of sequences -(deftype ::stream-flatten stream-flatten-state) +(deftype- ::stream-flatten stream-flatten-state) (defstream ::stream-flatten [[buffer stream]] diff --git a/src/clojure/contrib/types.clj b/src/clojure/contrib/types.clj index 53cdb816..35d2fa53 100644 --- a/src/clojure/contrib/types.clj +++ b/src/clojure/contrib/types.clj @@ -25,16 +25,20 @@ (defmacro deftype "Define a data type by a type tag (a namespace-qualified keyword) - and a symbol naming the constructor function. Optionally, a pair - of constructor and deconstructor functions can be given as well, - the defaults are clojure.core/identity and clojure.core/list. - The full constructor associated with constructor-name calls the - constructor function and attaches the type tag to its result - as metadata. The deconstructor function must return the arguments - to be passed to the constructor in order to create an equivalent - object. It is used for printing and matching." + and a symbol naming the constructor function. Optionally, a + constructor and a deconstructor function can be given as well, + the defaults being clojure.core/identity and clojure.core/list. + The full constructor associated with constructor-name calls the + constructor function and attaches the type tag to its result + as metadata. The deconstructor function must return the arguments + to be passed to the constructor in order to create an equivalent + object. It is used for printing and matching." ([type-tag constructor-name] - `(deftype ~type-tag ~constructor-name identity list)) + `(deftype ~type-tag ~constructor-name + clojure.core/identity clojure.core/list)) + ([type-tag constructor-name constructor] + `(deftype ~type-tag ~constructor-name + ~constructor clojure.core/list)) ([type-tag constructor-name constructor deconstructor] `(do (derive ~type-tag ::type) @@ -44,6 +48,13 @@ (defmethod deconstruct ~type-tag [~'x] (~deconstructor (with-meta ~'x {}))))))) +(defmacro deftype- + "Same as deftype but the constructor is private." + [type-tag constructor-name & optional] + `(deftype ~type-tag + ~(vary-meta constructor-name assoc :private true) + ~@optional)) + (defmethod print-method ::type [o w] (print-method (cons (::constructor ^o) (deconstruct o)) w)) |