Skip to content

Instantly share code, notes, and snippets.

@marcinkoziej
Created February 19, 2011 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcinkoziej/835525 to your computer and use it in GitHub Desktop.
Save marcinkoziej/835525 to your computer and use it in GitHub Desktop.
This defines multimethods, and the other implements them
(ns a
(:use m))
(defmethod foo :a [o] (str "a-method for " o))
(ns m
(:require a))
(defstruct ob :baz :bar)
(defmulti foo :baz)
@marcinkoziej
Copy link
Author

The problem:
I would like to put defmethods in separate files, depending on dispatch value (:baz).
I'd like to be able to add new implementations (files) for new :baz values, so my methods are extensible.

In this example I'd like to be albe to
(use 'm)
(foo (struct ob :a 123))

but this will not work, since there is a circular dependency.
On the other hand when I'll remove (:require a) from m.clj, I will have to do
(use 'm 'a)
(foo (struct ob :a 123))

and if I add new implementations in b.clj, I will have to change all my files to: (use 'm 'a 'b)

How should one handle this use case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment