Permalink
Browse files

Separate definition from extension mechanism.

  • Loading branch information...
1 parent 0eedcf8 commit be13b51c7e2b2970c267b6f4eb3909928db678dc @pjstadig committed Oct 9, 2012
Showing with 31 additions and 14 deletions.
  1. +31 −14 src/name/stadig/polyfn.clj
@@ -89,20 +89,37 @@
cache))))
(defmacro defpolyfn
- ([name type params & body]
+ ([name params]
(let [[target & args] params]
- `(do (defonce ~name
- ^{::dispatch (atom {})
- ::var (var ~name)}
- (fn polyfn# ~params
- (let [cache# (.__methodImplCache ^AFunction polyfn#)
- f# (.fnFor cache# (clojure.lang.Util/classOf ~target))]
- (if f#
- (f# ~@params)
- ((-cache-polyfn-fn ~name ~target) ~@params)))))
- (swap! (::dispatch (meta ~name))
- assoc ~type (fn [~(with-meta target {:tag type})
+ `(defonce ~name
+ ^{::dispatch (atom {})
+ ::var (var ~name)}
+ (fn polyfn# ~params
+ (let [cache# (.__methodImplCache ^AFunction polyfn#)
+ f# (.fnFor cache# (clojure.lang.Util/classOf ~target))]
+ (if f#
+ (f# ~@params)
+ ((-cache-polyfn-fn ~name ~target) ~@params))))))))
+
+(defn add-impl [polyfn type f]
+ (swap! (::dispatch (meta polyfn)) assoc type f)
+ (reset-cache polyfn)
+ polyfn)
+
+(defn remove-impl [polyfn type]
+ (swap! (::dispatch (meta polyfn)) dissoc type)
+ (reset-cache polyfn)
+ polyfn)
+
+(defn reset-polyfn [polyfn]
+ (reset! (::dispatch (meta polyfn)) {})
+ (reset-cache polyfn)
+ polyfn)
+
+(defmacro extend-polyfn [name type params & body]
+ (let [[target & args] params]
+ `(do (add-impl ~name ~type (fn [~(with-meta target {:tag type})
~@args]
~@body))
- (reset-cache ~name)
- (var ~name)))))
+ (reset-cache ~name)
+ (var ~name))))

0 comments on commit be13b51

Please sign in to comment.