clojure-spec

2024-01-04T15:53:52.169109Z

I'm a bit rusty with spec. Is there a "trick" to get generators working across s/and? I think I get why it must behave like this, and can think of a few ways to do this but not sure the best or if there's some built-in shortcut in the generators library. Or is it better just to keep s/keys specs "top level" when possible?

(s/def ::x string?)
  (s/def ::y keyword?)
  (s/def ::a (s/keys :req [::x]))
  (s/def ::b (s/keys :req [::y]))
  (s/exercise ::a) ;;works
  (s/exercise ::b) ;;works
  (s/exercise (s/and ::a ::b)) ;;ExceptionInfo Couldn't satisfy such-that predicate after 100 tries.

✅ 1
phronmophobic 2024-01-04T16:02:04.621139Z

I believe you want https://clojuredocs.org/clojure.spec.alpha/merge

2024-01-04T16:03:08.782539Z

Ah yes perfect, thanks!

👍 1
Alex Miller (Clojure team) 2024-01-04T16:46:27.013049Z

yes, merge is what you want here