Fork me on GitHub

Can I add spec to a function before I define the function? like @spec in elixir. And is it possible to have a variable T to represent some type like generic types?


@doglooksgood Not sure what you mean about "generic types" in the context of Clojure?


as for declaring a spec for a function before its defn, yes, that definitely possible...


my mistake, I was thought that I have to write spec for function after that is defined. for my first question, maybe I shouldn't call it a generic type. I want to know something that can help me writing a spec for functions like map.


map is a pretty hard function to write a spec for -- especially since the one argument version returns a transducer 🙂


You don't need to spec everything. Just spec at the boundaries of your subsystems and/or APIs.


it seems impossible for writing spec for a function that receive T and returns T?

Alex Miller (Clojure team)16:08:30

correct - specs are not parameterized


I think it doesn't make sense to have parameterized specs. Because they're predicate based, the parameters would be non intuitive. For each one you'd have to pass in a pred, but you wouldn't know how that pred would combine with the spec. And each spec could do it differently. That said, I'm not sure, it's a good idea. I'm trying to think if there's useful real use cases for it, nothing jumps at me right away, can you suggest one?

Alex Miller (Clojure team)18:08:04

well, we’re not going to do it, so I’m not going to invent a reason why :)


I think spec is much more valuable for spec'ing data structures than functions, to be honest, but that might be the domain I'm working in (where I'm using spec mostly to describe what API parameters should be, so s/conform and s/invalid? are being called).


So you want the type of the return to match the type of the first argument?


so I should use :fn in fdef?


You can spec that with :fn to check that yes.


Do you really want identical types tho'? What sort of function are you talking about?


For example, map takes a function and a collection and returns a lazy sequence so the types don't match there -- but I guess you could check non-empty collection/sequence members being the same type? But you can't do much for empty collection/sequence. Would you want to check the whole input/output? What about lazy sequences not being entirely consumed, like (take 5 (map inc (range)))?


You need to be careful that the spec doesn't realize the whole sequence.


@doglooksgood :fn will not be checked by instrumentation. you will need you run tests explicitly.


I got a library for this


But I think it will be nice to have some spec inference in editor. but without type variable, it seems to be difficult.


@doglooksgood I'd give a look at It gives everything you spec a type based on the spec, and then uses that to validate types at compile time. Maybe you could use it to infer things in an editor.


I’m slowly working on inference of untyped fns


also, spectrum isn’t really usable for production use yet


Functions with the signature like +: (fn [& args])... How to declare theirs specs? (s/fdef my-fn :args (s/coll-of integer?)) ?


@souenzzo (s/cat (s/* integer?))


@souenzzo actually minus the cat, just (s/* integer?)


boot.user=> (defn +'' [& args] (apply + args))
boot.user=> (s/fdef +'' :args (s/* integer?))
boot.user=> (st/instrument)
boot.user=> (+'' 1 2 3)
boot.user=> (+'' 1 2 3 'a)
clojure.lang.ExceptionInfo: Call to #'boot.user/+'' did not conform to spec:
                            In: [3] val: a fails at: [:args] predicate: integer?