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
danieroux 2021-05-15T14:01:31.187200Z

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

alexmiller 2021-05-15T14:45:50.187500Z

It’s a macro iirc

alexmiller 2021-05-15T14:46:32.188500Z

I think there’s actually a ticket about this

alexmiller 2021-05-15T14:47:29.189800Z

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

1👍
danieroux 2021-05-15T15:37:09.190500Z

Thank you

danieroux 2021-05-15T16:22:41.191100Z

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

2021-05-15T16:33:43.191600Z

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

2021-05-15T16:34:02.192100Z

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

danieroux 2021-05-15T16:49:04.193400Z

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.

2021-05-15T16:51:37.194Z

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

2021-05-15T16:52:34.194500Z

well maybe not

2021-05-15T16:56:19.194800Z

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

danieroux 2021-05-15T17:00:36.195300Z

Thank you @gfredericks!

1👍
2021-05-15T17:00:48.195500Z

does it work does it work?

danieroux 2021-05-15T17:08:53.195800Z

It does!

danieroux 2021-05-15T17:09:33.196400Z

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

1❤️