A multimethod is created using a defmulti form, and
implementations of a multime- thod are provided by
defmethod forms. The mnemonic is that they come in
the same order as in the word multimethod itself:
first you define the multiple dispatch then the methods
to which calls are dispatched.
Alter (alter-var-root)
Atomically alters the root binding of var v by applying f to its
current value plus any args
Caused by: java.lang.ClassCastException: clojure.lang.MultiFn cannot be cast to clojure.lang.Var
The function wraped by defmulti is not actually fn but a MultiFn.
There’s a table in MultiFn; dispatch key as table key; dispatch
function as table value.
As we can use remove-method to amend this table:
That says, we need not alter the fn definition but assoc table value.
How to alter
Since clojure standard library miss add-method for MultiFn,
we have to write it outself just like remove-methodmethods that
already exists.
This is clojure language’s definition of MultiFn:
We just need to wrap it like this:
Now it’s so easy for us to alter a new function in multifn!
Usage
The macro allow us to write code in 2 style as mentioned in
prev post: