Fork me on GitHub
#clojure-spec
<
2024-06-12
>
evocatus15:06:55

Can I separate function definition (with defn) and schema (with fdef) into different namespaces?

delaguardo15:06:04

yes, you can register functional spec using resolvable symbol, either fully qualified or aliased

👍 1
evocatus15:06:38

That's great, thanks !

seancorfield16:06:18

Take a look at next.jdbc for an example of specs in one namespace, but function definitions in another (so they're optional for users).

evocatus15:06:24

How to spec (with fdef) multi-arity functions? https://clojure.org/guides/learn/functions#_multi_arity_functions UPD: it's described in fdef docs:

:args A regex spec for the function arguments as they were a list to be
  passed to apply - in this way, a single spec can handle functions with
  multiple arities
So, looks like we should use one schema to describe all arities at once

peterh17:06:52

I spec them like this:

(s/fdef foo
  :args (s/alt :ar1 (s/cat :arg1 ::my-spec)
               :ar2 (s/cat :arg1 ::my-spec
                           :arg2 ::my-other-spec)
               …
  :ret  …)
Where I usually use the argument names instead of "arg1" etc. for clarity.