public
Last active

Operating System dependent dispatch of multimethods in clojure

  • Download Gist
README.md
Markdown

This is me trying out clojure's multimethods for a problem I'm trying to solve. See this Stack Overflow question for follow up.

Note that implementation namespaces are named utils-linux and utils-windows instead of utils.linux and utils.windows because Gists can't have subdirectories.

main.clj
Clojure
1 2 3 4 5 6 7 8
(ns main
(:use utils))
 
(require (case current-os
:linux 'utils-linux
:windows 'utils-windows))
 
(where-am-i)
utils.clj
Clojure
1 2 3 4 5 6 7 8
(ns utils)
 
(def current-os (-> (System/getProperty "os.name")
.toLowerCase
keyword))
 
(defmulti where-am-i
(fn [& _] current-os))
utils_linux.clj
Clojure
1 2 3 4 5 6
(ns utils-linux
(:use utils))
 
(defmethod where-am-i :linux
[]
(prn (str "I am running on " (System/getProperty "os.name"))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.