Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created July 1, 2014 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alandipert/a383318cff6f21d309a3 to your computer and use it in GitHub Desktop.
Save alandipert/a383318cff6f21d309a3 to your computer and use it in GitHub Desktop.
user=> (ns there)
nil
there=> (defmulti ^:private doit identity)
#'there/doit
there=> (defmethod doit 1 [x] x)
#<MultiFn clojure.lang.MultiFn@d2de489>
there=> (doit 1)
1
there=> (in-ns 'user)
#<Namespace user>
user=> (defmethod there/doit 2 [x] x)
CompilerException java.lang.IllegalStateException: var: there/doit is not public, compiling:(NO_SOURCE_PATH:21:1)
user=> (defmacro definitely-defmethod
[multifn dispatch-val & fn-tail]
`(. @#'~(with-meta multifn {:tag 'clojure.lang.MultiFn}) addMethod ~dispatch-val (fn ~@fn-tail)))
#'user/definitely-defmethod
user=> (definitely-defmethod there/doit 2 [x] x)
#<MultiFn clojure.lang.MultiFn@d2de489>
user=> (in-ns 'there)
#<Namespace there>
there=> (doit 2)
2
there=>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment