Fork me on GitHub
#clojure-spec
<
2021-05-15
>
danieroux14:05:31

Is there a reason that clojure.test.check.generators/let was left out of clojure.spec.gen.alpha/lazy-combinators?

Alex Miller (Clojure team)14:05:32

I think there’s actually a ticket about this

Alex Miller (Clojure team)14:05:29

As a macro, you can’t do the same dynaload we do with the other combinators

👍 2
danieroux16:05:41

This is what I could come up with, it feels unwieldly:

(try
  (requiring-resolve 'clojure.test.check.generators/let)
  (catch Exception _))
(s/def ::prodquint
  (s/with-gen
    (s/and string? #(re-matches pro-dquint-regex %))
    (fn []
      (clojure.test.check.generators/let
        [pro-quint-hi (s/gen ::proquint)
         pro-quint-lo (s/gen ::proquint)]
        (str pro-quint-hi "-" pro-quint-lo)))))

gfredericks16:05:43

are you trying to produce code that still works if test.check isn't available? if so I can't see how your code accomplishes that

gfredericks16:05:02

and if not, then I'd think you could just require let in the more vanilla fashion and everything would be fine

danieroux16:05:04

That is what I am trying, and failing, to accomplish. I want the spec to be there with its generator attached to it. Knowing that the generator will only be used int dev/test.

gfredericks16:05:37

if you want the let call in the same spot as your spec then I think you basically end up needing eval or something equivalent

gfredericks16:05:34

well maybe not

gfredericks16:05:19

this might work

(defmacro tclet
  [& args]
  (let [e (try
            (requiring-resolve 'clojure.test.check.generators/let)
            nil
            (catch Exception e e))]
    (if e
      `(throw (Exception. ~(.getMessage e)))
      `(clojure.test.check.generators/let ~@args))))

gfredericks17:05:48

does it work does it work?

danieroux17:05:33

(turns out I had test.check on the prod classpath all along, now it breaks and works in expected ways)

❤️ 2