Fork me on GitHub
#malli
<
2023-05-25
>
ingesol16:05:48

Is it possible in malli to instrument a function on the fly? My use case is wanting to ensure symmetric re-frame db handlers that always receive and return the db. I would like to instrument programmatically in my reg-event function. Example:

(defn my-updater-event [db k v]
  (update db k v))

(instrument-this-fn my-updater-event [:=> [:cat :map [:* :any]] :map])

p-himik16:05:14

It's possible in CLJ with alter-var-root - and that's how built-in Malli instrumentation works. In CLJS, it might be possible with set!: https://clojurians.slack.com/archives/C03S1L9DN/p1604308689481100 But no clue whether it'll survive advanced optimizations and, even if it does now, whether it's something one can rely on. I would probably write a macro that works like defn but also accepts a schema and produces an already instrumented function.

ingesol16:05:34

yup, I did the macro experiment earlier and it’s pretty nice. Probably makes more sense.

escherize16:05:35

I havn’t tried but it looks like it’ll work in cljs. You can add :malli/always to the function’s metadata to make malli.. always check the input and outputs

ingesol07:05:27

Thanks, know about that one. The variant I want is a version of this macro that always enforces a certain schema, so the same but actually simpler.