metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.43k stars 204 forks source link

WIP: `:->` #1027

Closed frenchy64 closed 2 days ago

frenchy64 commented 3 months ago

Introduce a flat syntax for function arguments.

[:-> :any] ; [:=> :cat :any]
[:-> :int :any] ; [:=> [:cat :int] :any]
[:-> [:cat :int] :any]  ; [:=> [:cat [:cat :int]] :any]
[:-> a b c d :any] ; [:=> [:cat a b c d] :any]

;; guard property
[:-> {:fn (fn [[[arg] ret]] ...)}
 :arg :ret]
; [:=> [:cat :arg] :ret [:fn (fn [[[arg] ret]] ...)]]
frenchy64 commented 2 months ago

@ikitommi I took your idea and ran with it. Since you were unhappy with :function, I added a new proxy schema :ifn. LMK what you think.

[:function [:=> [:cat] :int] [:=> [:cat :int] :int]]
==
[:ifn [:-> :int] [:-> :int :int]]
ikitommi commented 2 days ago

Hey, finally had time to dig into this. I think the :ifn ~= :function gives too many ways to do one thing:

[:function [:=> :cat :int]]
[:function [:-> :int]]
[:ifn [:=> :cat :int]]
[:ifn [:-> :int]]

also, :ifn sound bit like ifn? which is a different thing, e.g. a map gives true to that.

otherwise, looks good! I'll pick up this branch and play with it + add if something is missing or would like to have it differently.

ikitommi commented 2 days ago

I dropped the AritySchema and moved the -function-info into the Function protocol + added some tests + renamed :fn property as :guard.

Few things I noticed:

  1. explain now explains on underlaying :=> schema, might be ok?
  2. there is no custom ast & to-ast for :->
ikitommi commented 2 days ago

I dropped :-> from the default registry and marked m/-->-schema as experimental due to AST and explain results potentially being unstable. But, good to go to master.

ikitommi commented 2 days ago

Thanks!!

frenchy64 commented 2 days ago

also, :ifn sound bit like ifn? which is a different thing, e.g. a map gives true to that.

I don't see the problem, that's also true for :function.

user=> (m/validate [:function [:=> [:cat [:enum 1]] [:enum 1]]] {1 1})
true