This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-04
Channels
- # 100-days-of-code (8)
- # announcements (4)
- # beginners (77)
- # boot (11)
- # business (13)
- # cider (69)
- # clara (2)
- # cljdoc (51)
- # clojure (59)
- # clojure-dev (18)
- # clojure-italy (4)
- # clojure-nl (11)
- # clojure-spec (54)
- # clojure-uk (115)
- # clojurescript (33)
- # core-async (4)
- # cursive (95)
- # datomic (27)
- # duct (1)
- # emacs (58)
- # figwheel (22)
- # figwheel-main (63)
- # garden (1)
- # graphql (10)
- # hyperfiddle (1)
- # leiningen (1)
- # luminus (6)
- # off-topic (12)
- # planck (7)
- # portkey (1)
- # quil (3)
- # re-frame (3)
- # reagent (5)
- # ring-swagger (3)
- # shadow-cljs (34)
- # slack-help (19)
- # spacemacs (57)
- # testing (2)
- # timbre (2)
- # tools-deps (42)
- # yada (6)
@scot-brown 1) https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html#clojure.spec.alpha/fdef
Note that :fn specs require the presence of :args and :ret specs to
conform values, and so :fn specs will be ignored if :args or :ret
are missing.
2) https://clojure.github.io/spec.alpha/clojure.spec.test.alpha-api.html#clojure.spec.test.alpha/instrument
does not validate against :ret and :fn fdef's spec-components, see: https://groups.google.com/d/msg/clojure/JU6EmjtbRiQ/uND70kAFBgAJ
The official way to test against :ret and :fn of s/fdef - is test.check.
Folks who accept performance price also use https://github.com/jeaye/orchestra instead of vanilla stest/instrumentso instrument
only validates the arglist and not the return value
Yes- it instruments functions to ensure you are invoking them correctly
If I have a lot of functions all with the same spec how can I share it between them? I’m guessing I can use an fspec
with fdef
somehow? I really want these specs to show up in the doc strings
Sure, name the common spec and use the name
oh nice. Thanks @alexmiller
So you can (s/def foo ::a)
where ::a is an s/fspec
There were some bugs around this but I think those are all fixed now
what is the right use? [clojure.spec.alpha :as s]
or [clojure.spec :as s]
. I tried [clojure.spec :as s]
but it seems no lib in this path
Spec is still alpha right now.
[clojure.spec.alpha :as s]
always the namespace with "alpha". Clojure 1.9-alpha had a brief period where spec was in clojure core (not a separate lib). That is why you may see non-alpha namespaces sometimes.
All of the Spec namespaces end in .alpha
-- until it stops being alpha.
@favila Ah, yeah, that must have been a fairly brief period 🙂
yes, I saw it https://www.youtube.com/watch?v=TD7VGSSZ3ng video
The great thing about REPL development is you can try these things very quickly and see which one works.
There's a comment on that video, dated a year ago, that says "Good introduction! But something changed in the meantime: starting from 1.9.0-alpha16 you have to include clojure.spec.alpha" @fabrao
@the2bears Aye, I always have a REPL open and I try stuff in it all the time.
https://clojure.org/community/devchangelog clojure 1.9.0-alpha16 is when it split. So from May 24 2016 to april 26, 2017 "clojure.spec" was the correct namespace
I left using lein for boot because of @seancorfield
Longer than I realized -- thanks @favila for that detective work!
Boot REPL is nice because you can easily add new dependencies without restarting the REPL!
@seancorfield my problem is I lose track of my REPL's state from time to time 🙂 . But I have 2 or 3 open at any one time, usually, main project then libraries used in it.
In this case, I got Assert failed
. Is there any way to custom error message?
(defn stringer-bell
"Prints a string and rings bell."
[s]
{:pre [(s/valid? (s/nilable string?) s)]}
(println s "\007"))
maybe relevant thread here about custom pre/post messages https://groups.google.com/forum/#!msg/clojure/xHrFyDcPS9A/gXiNY6pmAwAJ
I tried
(if (not (s/valid? ::campos campo))
(throw (Exception. (str "Tipo do campo incorreto. Válidos = #{" (reduce #(str %1 %2 " ") "" campos) "}"))))
also, if you think you need this type of check a lot, you could easily define a function that takes a spec, an input, and maybe an error message or explain-data-to-string function; and then wherever you call it should be pretty terse?
I´m thinking use https://github.com/bhb/expound to be more explicity
also check out https://github.com/bhb/expound if you haven't already, and I think there's a similar lib that's more geared towards user-facing error messages
if you are writing a s/fdef foo
but want to place it in a separate ns (not the same where foo
is), should you require the ns where the function is in ns where the spec is? But what if you need to use specs say for validation (in the ns where s/fdef is implemented) how do you avoid circular dependency?
or should you move higher order specs to a separate ns, but keep s/fdef foo
where foo
is implemented?
isn't some of this simplified by the fact that the spec is looked up by a keyword? you can use a namespaced keyword before the namespace it nominally refers to exists
by the time the spec is actually checked, you need the ns that defined the spec for that keyword to be loaded, of course