Skip to content

Commit

Permalink
Fix #112 (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul authored Sep 8, 2022
1 parent fd7a1a0 commit 6361ebe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/methodical/impl/multifn/standard.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
(let [dispatch-values (transduce
(comp cat
(map meta)
(map :dispatch-value)
(filter some?))
(filter #(contains? % :dispatch-value))
(map :dispatch-value))
conj
[]
(cons primary-methods (vals aux-methods)))]
Expand Down
8 changes: 4 additions & 4 deletions src/methodical/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
(aux-methods [method-table]
"Get a `qualifier -> dispatch-value -> [fn]` map of all auxiliary methods associated with this method table.")

(add-primary-method [method-table dispatch-value f]
(add-primary-method ^methodical.interface.MethodTable [method-table dispatch-value f]
"Set the primary method implementation for `dispatch-value`, replacing it if it already exists.")

(remove-primary-method [method-table dispatch-value]
(remove-primary-method ^methodical.interface.MethodTable [method-table dispatch-value]
"Remove the primary method for `dispatch-value`.")

(add-aux-method [method-table qualifier dispatch-value f]
(add-aux-method ^methodical.interface.MethodTable [method-table qualifier dispatch-value f]
"Add an auxiliary method implementation for `qualifer` (e.g. `:before`) and `dispatch-value`. Unlike primary
methods, auxiliary methods are not limited to one method per dispatch value; thus this method does not remove
existing methods for this dispatch value. existing ")

(remove-aux-method [method-table qualifier dispatch-val method]
(remove-aux-method ^methodical.interface.MethodTable [method-table qualifier dispatch-val method]
"Remove an auxiliary method from a method table. Because multiple auxiliary methods are allowed for the same
dispatch value, existing implementations of `MethodTable` are currently only able to remove exact matches -- for
functions, this usually means identical objects.
Expand Down
35 changes: 31 additions & 4 deletions test/methodical/impl/multifn/standard_test.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(ns methodical.impl.multifn.standard-test
(:require [clojure.math.combinatorics :as combo]
[clojure.test :as t]
[methodical.core :as m]
[methodical.impl.multifn.standard :as standard]))
(:require
[clojure.math.combinatorics :as combo]
[clojure.test :as t]
[methodical.core :as m]
[methodical.impl.multifn.standard :as standard]))

(set! *warn-on-reflection* true)

(derive ::parrot ::bird)
(derive ::parakeet ::parrot)
Expand Down Expand Up @@ -205,3 +208,27 @@
:default
[expected-1 expected-2])}
(meta (standard/standard-effective-method combo dispatcher method-table dv)))))))))

(t/deftest nil-dispatch-values-test
(t/testing "Dispatch values for `nil` should be calculated correctly (#112)"
(doseq [order [[nil :default]
[:default nil]]]
(t/testing (str "\norder = " (pr-str order))
(let [mf* (atom nil)
mf (-> (m/multifn
(m/standard-multifn-impl
(m/thread-last-method-combination)
(m/multi-default-dispatcher identity)
(m/standard-method-table)))
(m/add-primary-method :default (fn [_next-method _m] :default))
(m/add-primary-method nil (fn [_next-method _m]
(@mf* :default)
nil)))]
(reset! mf* mf)
(doseq [x order]
(t/testing (str \newline (pr-str x))
(t/testing (str \newline `m/effective-dispatch-value)
(t/is (= x
(m/effective-dispatch-value mf x))))
(t/is (= x
(mf x))))))))))

0 comments on commit 6361ebe

Please sign in to comment.