malli icon indicating copy to clipboard operation
malli copied to clipboard

WIP: `:->`

Open frenchy64 opened this issue 1 year ago • 1 comments

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]] ...)]]
  • [x] disallow :-> without children, looks like an infix op [:=> :a :-> :b].
  • [ ] test :fn property
  • [ ] add generators for :ifn :->

frenchy64 avatar Mar 28 '24 16:03 frenchy64

@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]]

frenchy64 avatar Apr 14 '24 05:04 frenchy64

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 avatar Jun 30 '24 11:06 ikitommi

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 avatar Jun 30 '24 13:06 ikitommi

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 avatar Jun 30 '24 13:06 ikitommi

Thanks!!

ikitommi avatar Jun 30 '24 13:06 ikitommi

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

frenchy64 avatar Jun 30 '24 16:06 frenchy64