fennel-xl/multimethod.fnl
2022-12-03 23:26:07 -05:00

22 lines
646 B
Fennel

(local util (require :plugins.fennel-xl.util))
(local mm {})
(fn mm.__call [{: module : name} ...]
(let [dispatcher (. mm.dispatchers module name)
key (dispatcher ...)
method (or (. mm.methods module name key) (. mm.methods module name :default))]
(method ...)))
(fn mm.defmulti [dispatcher name module]
(util.nested-tset mm [:dispatchers module name] dispatcher)
(setmetatable {: module : name} mm))
(fn mm.defmethod [{: module : name} key method]
(util.nested-tset mm [:methods module name key] method))
(fn mm.keys [{: module : name}]
(icollect [key _ (pairs (or (?. mm :methods module name) []))] key))
mm