4

scenario: i want to use multimethods to dispatch and spread functionality across multiple files. one of the files contains only multimethods, and to make them usable i must manually load the file. is there a way to automatically load the file, rather than explicitly loading it?

here's a simple example of what i'm doing:

; app/core.clj
(ns app.core
  (:use [app.fruit.core :only [make-fruit]])
(println (:name (make-fruit :banana)))

; app/fruit/core.clj
(ns app.fruit.core)
(defmulti make-fruit identity)

; app/fruit/banana.clj
(ns app.fruit.banana
  (:use [app.fruit.core :only [make-fruit]])
(defmethod make-fruit :banana [fruit]
  {:name "banana" :color "yellow})

fruit.banana's method aren't loaded unless i explicitly load it in app.core, like adding it to the :use group. this seems to defeat the purpose of using a multimethod since i still have to be explicit about all the methods implementing it.

xen
  • 133
  • 6
  • If wildcarding namespaces were allowed then this would be trivial. Follow this thread http://stackoverflow.com/q/1990714/1552130 – KobbyPemson Feb 18 '14 at 21:51

2 Answers2

0

You don't have to be explicit unless you want to be. The methods will work if they are loaded at another point by something else. If I want to add a method to make-fruit, then I would have to load that method myself. At that point, any code that calls make-fruit, will be aware of my new method.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • i guess i understand that much, but my problem is that nothing else in the file is being loaded; it only consists of multimethod implementations. so at some point i have to explicitly load it just so that the methods become available. if it's not possible to avoid that, i might consider using functions like make-banana instead of the generic form if i have to be explicit anyway. – xen Feb 18 '14 at 17:41
0

What you could try is this at the bottom of app/fruit/core.clj:

(load "banana")
; Add other fruits here.

To make it more flexible, you can of course enumerate all files although, sadly, clojure.core/root-directory is private.

Marcin Bilski
  • 572
  • 6
  • 13