fennel-xl/multimethod.fnl

22 lines
646 B
Plaintext
Raw Normal View History

2022-12-04 04:26:07 +00:00
(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