Fork me on GitHub
#malli
<
2022-03-07
>
henrik14:03:33

Is it possible to spec multi arity functions with the experimental Plumatic-style schema? https://github.com/metosin/malli/blob/master/docs/function-schemas.md#function-inline-schemas

ikitommi14:03:41

sure:

(mx/defn plus
  ([x :- :int] x)
  ([x :- :int, y :- :int] (+ x y))
  ([x :- :int, y :- :int, & zs :- [:* :int]] (apply + x y zs)))

henrik14:03:29

Doh, naturally, thank you.

henrik13:03:57

My head was not screwed on right yesterday. What I meant to ask was, how do I scope a particular return spec to a particular arity. In e.g. Snoop, this is possible since the return spec is part of the arity. In Plumatic, I'm unsure what the syntax is.

ikitommi15:03:17

there is no such thing in the Plumatic Syntax. Thought of adding that after the args-vector, but then it would not be Plumatic Syntax anymore.

ikitommi15:03:55

I propose to ask this from the plumatic schema gang via an issue, happy to incorporate whatever they think is good, could add my 2 cents in the conversation there

ikitommi15:03:03

… and you can do that with non-plumatic syntax already.

ikitommi15:03:53

(defn f1
  "doc"
  (^{:malli/schema [:=> [:cat :int] :int]} [x] (inc x))
  (^{:malli/schema [:=> [:cat :int :int] :int]} [x y] (+ x y)))
or
(defn f2
  "doc"
  {:malli/schema [:function
                  [:=> [:cat :int] :int]
                  [:=> [:cat :int :int] :int]]}
  ([x] (inc x))
  ([x y] (+ x y)))

👍 1
henrik08:03:19

I think it would be good to alias the Plumatic macro name to something like >defn as well. Partly because it seems to be a bit of a convention, partly because it makes it possible to refer it without breaking the equivalent in clojure.core.

henrik08:03:35

Regarding arity-specific return specs; to me it makes sense to tack them on to the param vectors. It might even be possible to combine the two: a spec that runs regardless of the arity, and a spec that runs per arity.

ikitommi09:03:55

Personally, I really don’t like the >defns. clojure.test is the only ns that I require macros without namespace alias. mx/defn tells where it is coming from.

ikitommi09:03:56

(mx/defn plus :- :int ;; the default, masked by 2 arities? 
  ([x :- :int] :- :int x)
  ([x :- :int, y :- :int] (+ x y))
  ([x :- :int, y :- :int, & zs :- [:* :int]] :- :int (apply + x y zs)))

ikitommi09:03:02

starting to be quite verbose. Have asked if Cursive could grey out the schema hints so it would be move visbile

henrik11:03:07

> Personally, I really don’t like the >defns Fair enough. I tend to lean towards referring, when it's canonical or conventional (we decide on one >defn, and overloading it is an error), and >defn has become something of a naming convention among function spec libraries. > verbose Agreed, and it's also not that easy to make a formatter understand the syntax to begin with. The separate vector approach of Snoop/Guardrails etc. has the advantage of registering as a conventional function in terms of arguments fed to it. By contrast, Plumatic seems like it requires quite a bit of interpretation of syntax, especially when extended in this way. It also has advantages, of course.