clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
2020-08-24T19:40:18.051500Z

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?)?

seancorfield 2020-08-24T19:46:57.052400Z

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

2020-08-24T19:47:40.052500Z

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

seancorfield 2020-08-24T19:47:58.053Z

https://clojure.org/guides/spec#_custom_generators

2020-08-24T19:53:13.053200Z

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

alexmiller 2020-08-24T19:55:20.053400Z

yeah, this is not currently an open set

alexmiller 2020-08-24T19:57:26.053600Z

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).

2020-08-24T20:01:30.053900Z

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