From 37606d41d3dcc574dd6b755de4849fe899c01af2 Mon Sep 17 00:00:00 2001 From: Rich Hickey Date: Wed, 1 Apr 2009 12:39:39 +0000 Subject: [PATCH] added get-method --- src/clj/clojure/core.clj | 5 +++++ src/jvm/clojure/lang/MultiFn.java | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index f44fcb71a5..934dbe89fa 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -1085,6 +1085,11 @@ "Given a multimethod, returns a map of dispatch values -> dispatch fns" [#^clojure.lang.MultiFn multifn] (.getMethodTable multifn)) +(defn get-method + "Given a multimethod and a dispatch value, returns the dispatch fn + that would apply to that value, or nil if none apply and no default" + [#^clojure.lang.MultiFn multifn dispatch-val] (.getMethod multifn dispatch-val)) + (defn prefers "Given a multimethod, returns a map of preferred value -> set of other values" [#^clojure.lang.MultiFn multifn] (.getPreferTable multifn)) diff --git a/src/jvm/clojure/lang/MultiFn.java b/src/jvm/clojure/lang/MultiFn.java index d7b089ce7f..1efa84b6a4 100644 --- a/src/jvm/clojure/lang/MultiFn.java +++ b/src/jvm/clojure/lang/MultiFn.java @@ -93,7 +93,7 @@ private IPersistentMap resetCache() throws Exception{ return methodCache; } -synchronized private IFn getFn(Object dispatchVal) throws Exception{ +synchronized public IFn getMethod(Object dispatchVal) throws Exception{ if(cachedHierarchy != hierarchy.deref()) resetCache(); IFn targetFn = (IFn) methodCache.valAt(dispatchVal); @@ -103,6 +103,11 @@ synchronized private IFn getFn(Object dispatchVal) throws Exception{ if(targetFn != null) return targetFn; targetFn = (IFn) getMethodTable().valAt(defaultDispatchVal); + return targetFn; +} + +private IFn getFn(Object dispatchVal) throws Exception{ + IFn targetFn = getMethod(dispatchVal); if(targetFn == null) throw new IllegalArgumentException(String.format("No method for dispatch value: %s", dispatchVal)); return targetFn;