Fork me on GitHub
#clojure-spec
<
2020-08-24
>
Lennart Buit19:08:18

So spec (1) has a list of predicates it can create generators for, so (s/gen int?) works. I kinda forgot, is it possible to extend this on the predicate level, e.g. can I register a predicate so that this works (s/gen my-predicate?)?

seancorfield19:08:57

@lennart.buit You'd have to write your own generator (and then supply it when you define the spec based on your predicate).

Lennart Buit19:08:40

Yah right, so I can only define a spec with s/def and attach a generator that way

Lennart Buit19:08:13

Yeah right, reason I’m asking, we have quite a lot of specs that use a (non-spec aware) predicate, e.g. (s/def ::my-spec my-predicate?). So I was kinda hoping I wouldn’t have to do like:

(s/def ::my-predicate-spec (s/with-gen my-predicate? (fn [] ...))
(s/def ::my-spec ::my-predicate-spec)
But what I’m finding in the source, I appear to be out of luck

alexmiller19:08:20

yeah, this is not currently an open set

alexmiller19:08:26

I think the issue is that if you could extend it, then predicates may gen in one place but not in another if you didn't extend it the same way (somewhat reminiscent of reader macros).

Lennart Buit20:08:30

Right — because you have this global idea of what generators exists for what predicates and that could clash, or not be loaded or … Yeah I can see that as causing pains