Fork me on GitHub
#malli
<
2021-05-18
>
Setzer2209:05:31

The syntax to define multi-arity fns is this one, right?

[:function
  [:=> [:cat int? int?] int?]
  [:=> [:cat int?] string?]]

Setzer2210:05:01

Ok, so I was asking because I'm implementing multi-arity schema support in my malli-instrument library, but I'm unsure how to best deal with instrumenting multi-arity fns with varargs. Due to the way in which varargs are specified, I don't have a way to know which of the arities under :function corresponds to each of the possible lengths of the arg list, or at least not without some analysis, consider the following:

[:function 
  [:=> [:* int?] int?]
  [:=> [:* string?] string?]]
this is a valid schema for:
(defn foo
  ([x y z] (+ x y z))
  ([a b c d & _] (str a b c d)))
it can be argued that it's an innacurate schema for the function, but that's not my point: Regardless of the example, the fact remains that with seqex patterns being used to define arglists, one can't easily determine which one of the annotated arities to take given an arglist and its length :thinking_face: The only way to do this with the current specification is to test each of the arity specs against the arglist until some validates. This is what I'm implementing now, but I thought it'd be good to bring out this discussion nontheless, in case you think the specification format could be improved 👍

Setzer2211:05:22

I'm confused, what's this second one doing? 😅

Setzer2211:05:31

nice, thanks! 👍 I was re-inventing the wheel for some of this work in a more inefficient way so this helps a lot

Setzer2213:05:49

just noticed a small inconsistency when writing the instrumentation code: (m/validate :cat nil) returns false, but when calling (fn [& args]) with zero arguments, args is bound to nil

Setzer2213:05:22

I can easily work around it on my side using something like (or args '()), but I thought that may be something worth looking into

Setzer2213:05:52

alright! malli-instrument now supports multi-arity fn schemas 😄 🎉 https://github.com/setzer22/malli-instrument I'll continue working on this and pushing bugfixes

👍 9