This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-04
Channels
- # 100-days-of-code (1)
- # announcements (1)
- # beginners (51)
- # boot (1)
- # calva (1)
- # cider (16)
- # cljsrn (2)
- # clojure (52)
- # clojure-spec (19)
- # clojure-uk (8)
- # clojurescript (8)
- # events (1)
- # figwheel-main (8)
- # fulcro (57)
- # midje (1)
- # nrepl (10)
- # off-topic (38)
- # pedestal (4)
- # portkey (1)
- # reagent (3)
- # reitit (4)
- # spacemacs (8)
I'm writing a macro where I want to turn the passed in symbol/keyword into a spec. How do I do that?
yeah but that doesn't work for predicates
like someone passes in string?
s/get-spec
of course says that no such spec has been registered
Does creating a ::string spec work? @U08EKSQMS just made a PR to speculative for a lot of those
it sounds like you want to check if the symbol resolves to a function (predicate), and then make a spec out of that function?
The thing is that it's a library so I don't know what people will be passing in.
Yes something like that
creating spec out of a symbol has the benefit of having that symbol show up in description of the spec failure
this doesn’t really help, just an observation re: names/descriptions in failures:
(s/explain string? 0)
val: 0 fails predicate: :clojure.spec.alpha/unknown
=> nil
(s/explain (s/every string?) [0])
In: [0] val: 0 fails predicate: string?
=> nil
I see
(defmacro ->spec [k]
`(or (s/get-spec ~k) (s/spec ~k)))
(defn foo? [x] (= "foo" x))
=> #'?
(s/explain (->spec foo?) "foo")
Success!
=> nil
(s/explain (->spec foo?) "bar")
val: "bar" fails predicate: foo?
=> nil
(s/def ::foo #{"foo"})
=> :
(s/explain (->spec ::foo) "foo")
Success!
=> nil
(s/explain (->spec ::foo) "bar")
val: "bar" fails spec: : predicate: #{"foo"}
=> nil
cool I'll check it out
why do you need it in the first place? to have named things in explain's out? @U66G3SGP5
yes 🙂