Fork me on GitHub
#malli
<
2021-06-22
>
Yehonathan Sharvit09:06:43

What's the recommended location for function schema definition? Should it be on the same namespace as the function or in a separate namespace?

ikitommi14:06:48

No strong opinions in malli for that. My favourites: 1. Inlined (the plumatic syntax) 2. just before the functions (`m/=>` is as long as defn , looks good

ikitommi14:06:55

what do you think?

Yehonathan Sharvit14:06:38

Someone reviewed my code that added m/=> for each and every function in the core namespace of a lib (around 10 functions) and he thought it was polluting the namespace

Yehonathan Sharvit14:06:54

I tend to prefer to have the schemas near the code as schemas serve also as documentation

eskos15:06:14

I prefer the Plumatic style as it's more about upfront (meta) data expression, m/=> feels unnecessarily clever and sort of indirect/IoC hellish since it comes after the function declaration. But that's just my two cents 🙂

Lucy Wang14:06:58

+1 for this. Being able to place the schema right near to the argument is way more intuitive and maintainable than having them separate from each other.

emccue15:06:50

One option would be this

emccue15:06:33

make an aspect for it

emccue15:06:22

^{::m/aspects [(contract [:=> [:cat int? int?] string?])]}
(m/defn some-fn [x y]
  (str (+ x y))

Yehonathan Sharvit15:06:16

Does malli support plumatic style? how?

respatialized17:06:23

I'm wondering whether function metadata maps may be suited to this purpose, the way spec does with :pre/:post validation inlined in an ordinary defn form, without needing a macro for function definitions. There may be a good reason to not overload the "canonical" keywords and instead use something like :malli/schema to inline it, but having it right in the form using metadata allows tests to be easily derived from a self-describing function with meta. https://clojure.org/guides/spec Was there something in your experience with spec-tools that led you to believe this isn't a good idea or the optimal solution for malli?

ikitommi17:06:16

the plumatic syntax has been out since 2013 and I believe it's the most used syntax, by far. I have used it, it's just good.

ikitommi17:06:23

I like also the`:malli/schema` suggestion, the malli.instrument could be configured to support multiple sources like: 1. the => registry 2. the :malli/schema metadata 3. plain old function schema inferrer

2
ikitommi17:06:05

and, to be compliant: 4. the spec registry

Yehonathan Sharvit16:06:10

@ikitommi In your previous message, you wrote: > just before the functions (`m/=>` is as long as `defn` , looks good What do you mean by m/=> before the function? In malli's README, m/=> comes after the function definition

ikitommi16:06:18

this works too:

(m/=> plus [:=> [:cat [:* :int]] :int])
(defn plus [& ns] (apply + ns))

(plus 1 2 3)
; => 6

Yehonathan Sharvit16:06:59

Nice. In fact malli treats function names as symbols

Yehonathan Sharvit16:06:19

So it doesn't matter if you create the schema before the function is defined

respatialized17:06:23

I'm wondering whether function metadata maps may be suited to this purpose, the way spec does with :pre/:post validation inlined in an ordinary defn form, without needing a macro for function definitions. There may be a good reason to not overload the "canonical" keywords and instead use something like :malli/schema to inline it, but having it right in the form using metadata allows tests to be easily derived from a self-describing function with meta. https://clojure.org/guides/spec Was there something in your experience with spec-tools that led you to believe this isn't a good idea or the optimal solution for malli?