plumatic / schema

Clojure(Script) library for declarative data description and validation
Other
2.4k stars 256 forks source link

Minimize the cost of `s/fn-schema` support for `s/letfn` #431

Open frenchy64 opened 2 years ago

frenchy64 commented 2 years ago

https://github.com/plumatic/schema/pull/430 revealed a faster way to support s/fn-schema via metadata. We should look into the performance improvements of using the same strategy for s/letfn and then perhaps deprecate schematize-fn.

Idea:

(s/letfn [(f [a ...])]
  ...)
=>
(letfn [(f [a ...])]
  (let [f ^{:schema ...} (fn [a ..] (f a ...)))]
    ...))
frenchy64 commented 2 years ago

Another idea, but unfortunately it doesn't work:

user=> (letfn* [f ^:a (fn f [])] (meta f))
CompilerException java.lang.ClassCastException: class clojure.lang.Compiler$MetaExpr cannot be cast to class clojure.lang.Compiler$ObjExpr (clojure.lang.Compiler$MetaExpr and clojure.lang.Compiler$ObjExpr are in unnamed module of loader 'app'), compiling:(/private/var/folders/s5/309818k92r92zn9c_nzvmnzc0000gn/T/form-init5989770558247704827.clj:1:1)