Skip to content

Instantly share code, notes, and snippets.

Last active June 21, 2016 09:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Method missing in clojure
;;namespace test
(ns missing-test)
(defn method-missing [func args]
(println "missing '" func "' with args:" args)
[func args])
(defn hello [name]
(str "hello," name))
;;example namesapce
(ns missing-example
(:require [missing-test :as t]))
(defn- this-ns? [ns f]
(let [n (str f)]
(when (.contains n "/")
(let [nsm (-> n (.split "/") first symbol)]
(= ns (get (ns-aliases *ns*)
(catch Exception e
(defn- method-missing? [ns f]
(and (nil? (resolve f))
(get (ns-publics ns)
(catch Exception e
(defmacro with-method-missing [ns & body]
(let [ns (eval ns)]
(fn [form]
(if (and (list? form)
(this-ns? ns (first form))
(method-missing? ns (first form)))
(list `apply (get (ns-publics ns)
(vec (cons
(name (first form))
(next form))))
;;invoke with method missing
(with-method-missing (the-ns 'missing-test)
(println (t/hello "dennis"))
(println (t/world "dennis")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment