Fork me on GitHub
#clojure-spec
<
2019-11-01
>
Nazral09:11:43

I'm trying to write a macro that among other things needs to generate new specs by wrapping or merging existing ones, and it doesn't work. I made the simple following example

(defmacro wrapspec
  [base-name base-spec]
  (let [n-name `(keyword (str ":wrapped-" '~base-name))
        new-spec (s/coll-of base-spec)]
    `(s/def ~n-name ~new-spec)))
I'm not sure why doesn't this work? I tried with a symbol instead of a keyword too, to no avail

Nazral10:11:56

I also tried with

n-name `(symbol (str (.name *ns*)) '~base-name)
to no avail

alexmiller12:11:26

did you look at the expansion?

alexmiller12:11:27

just as a general technique it's important to (pprint (macroexpand-1 '(wrapspec 'abc int?))) or whatever to see if it's doing what you want

alexmiller12:11:30

here s/coll-of is going to be a spec object, which is not what you want

alexmiller12:11:58

new-spec `(s/coll-of ~base-spec)

alexmiller12:11:09

probably closer but I'm just eyeballing this

Flo21:11:17

Anyone aware of issues with clojure.spec.alpha/gen in cljs (using shadow-cljs)? This snippet works well in clj repl:

(s/def ::foo string?)
(s/gen ::foo)
but fails in cljs with: Var clojure.test.check.generators/simple-type-printable does not exist, clojure.test.check.generators never required at eval (/js/compiled/cljs-runtime/cljs.spec.gen.alpha.js:1854:8)

Flo21:11:30

My project.clj has a dependency entry for [org.clojure/test.check "0.10.0"]

Flo21:11:38

Nevermind, after I explicitly required [clojure.test.check.generators] in the same file I'm (s/gen ..)ing, it works.