Current section
Files
Jump to
Current section
Files
src/clj/erlang/core.clje
(ns ^{:doc "Erlang core features [EXPERIMENTAL]"
:author "Juan Facorro"}
erlang.core)
;;;;;;; behaviour ;;;;;;;;;;;;;
(defn- defbehaviour*
[callbacks]
(let [fun-arity (fn [name] #(tuple (keyword name) (count %)))
find-callbacks (fn [[name & arities]]
(map (fun-arity name) arities))
find-optional (fn [[name & arities]]
(->> arities
(filter #(-> % meta :optional))
(map (fun-arity name))))
f #(->> callbacks (map %) (apply concat) clj->erl)]
`(defn* ~'behaviour_info
([:callbacks] '~(f find-callbacks))
([:optional_callbacks] '~(f find-optional)))))
(defmacro defbehaviour
"Defines Erlang behaviour callbacks for the current ns.
(defbehaviour
(foo [x])
(bar [x] ^:optional [x y] [x y x]))"
[& callbacks]
(defbehaviour* callbacks))
(defn behaviour-callbacks
"Returns a list of callbacks for the provided namespace
if it defines.
"
[ns & [optional?]]
(let [module (cond
(instance? clojerl.Namespace ns)
(-> ns ns-name keyword)
:else (-> ns name keyword))
callback-type (if optional? :optional_callbacks :callbacks)]
(try
(erlang/apply module
:behaviour_info
#erl(callback-type))
(catch _ _
()))))
(defmacro behaviours
"Indicate the Erlang behaviours the current ns will implement.
(behaviours
gen_server
gen_fsm
custom)"
[& names]
`(do ~@(map #(list 'behaviour* %) names)))