Skip to content

Commit

Permalink
added get-method
Browse files Browse the repository at this point in the history
  • Loading branch information
richhickey committed Apr 1, 2009
1 parent 487b713 commit 37606d4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/clj/clojure/core.clj
Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion src/jvm/clojure/lang/MultiFn.java
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 37606d4

Please sign in to comment.