summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2009-04-01 12:39:39 +0000
committerRich Hickey <richhickey@gmail.com>2009-04-01 12:39:39 +0000
commit37606d41d3dcc574dd6b755de4849fe899c01af2 (patch)
tree624e680715644ad36c4b7608bc7934ee4728e575
parent487b713ce0f6fe49470a7f0aa7acf14bdb58c985 (diff)
added get-method
-rw-r--r--src/clj/clojure/core.clj5
-rw-r--r--src/jvm/clojure/lang/MultiFn.java7
2 files changed, 11 insertions, 1 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index f44fcb71..934dbe89 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 d7b089ce..1efa84b6 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;