This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-06-04
Channels
- # announcements (4)
- # beginners (110)
- # boot (6)
- # calva (23)
- # cider (14)
- # cljdoc (5)
- # cljs-dev (50)
- # cljsrn (3)
- # clojure (105)
- # clojure-europe (3)
- # clojure-italy (46)
- # clojure-nl (6)
- # clojure-spec (19)
- # clojure-sweden (1)
- # clojure-uk (78)
- # clojurescript (66)
- # core-async (5)
- # cursive (19)
- # data-science (16)
- # datomic (3)
- # events (2)
- # fulcro (11)
- # hoplon (53)
- # jobs (4)
- # jobs-discuss (6)
- # keechma (51)
- # leiningen (3)
- # nrepl (25)
- # off-topic (95)
- # parinfer (8)
- # precept (1)
- # reitit (61)
- # remote-jobs (1)
- # rewrite-clj (75)
- # ring-swagger (2)
- # robots (3)
- # shadow-cljs (43)
- # tools-deps (28)
- # vim (2)
Hello, how do I spec protocol methods? Just (s/fdef protocol-method :args (s/cat :this any? ...))
?
You'll have to wrap the protocol method into a spec-able function. E.g. using defn-spec
:
(defprotocol Protocol
(-foo [_ x]))
(ds/defn-spec foo
{::s/args (sp/pos any? ::specs/x)
::s/ret ::specs/y}
[this x]
(-foo this x))
@U0HJD63RN ah, it's a local helper to reduce verbosity:
#?(:clj
(defmacro -cat
[& body]
(if (-cljs-env? &env)
`(cljs.spec.alpha/cat ~@body)
`(clojure.spec.alpha/cat ~@body))))
#?(:clj
(defmacro pos
"Shorthand for s/cat but without specifying key for every position.
Name means \"positional\".
Keys are generated automatically: :pos1, :pos2, etc.
Convenient in ::s/args so that you don't have to keep s/cat keys in sync with function argument names.
Why not just use s/tuple instead? Because it doesn't support regex ops (e.g. s/?)."
[& pred-forms]
(let [keys-pred-forms (apply concat
(for [[i pred-form] (map-indexed vector pred-forms)]
[(keyword (str "pos" (inc i))) pred-form]))]
`(-cat ~@keys-pred-forms))))
ah nice - I had a feeling that was how it worked, just got my hopes up it might be something in spec-2 😞
My use case is a recursive tree-like spec that I’m generating using (gen/generate (s/gen ::my-spec))
there are some dynamic var knobs in the spec.alpha namespace
also, if you have any collection specs, I usually use :gen-max 3
to limit nested collections from going out of control
even given those, I think there are some known issues where the recursion is not well controlled
so for a string? bool in a macro, I get
{:path [:define-fx-params :orc-string],
:pred clojure.core/string?,
:val
(str
(slurp
(io/resource
"panaeolus/csound/fx/udo/shred.udo"))
"\ngkTransPose init 1\ngkTransRand init 0.1"),
:via [:panaeolus.csound.macros/orc-string],
:in [:orc-string]}
So it's obviously a string, but it's a macro, so I understand it sees a list at this point, any good tip to make this string check?By eval‘ing here you’re basically ruining the lazy evaluation of macros and could even cause issues
In general macro specs are often tricky to write unless you’re trying to enforce positional constraints
For something like this I would probably not spec it at all
But would spec that arg as any?
if I was