This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-09
Channels
- # announcements (1)
- # atlanta-clojurians (1)
- # beginners (198)
- # calva (4)
- # cider (16)
- # clara (8)
- # cljs-dev (14)
- # cljsrn (4)
- # clojure (204)
- # clojure-europe (3)
- # clojure-gamedev (2)
- # clojure-italy (8)
- # clojure-nl (17)
- # clojure-poland (3)
- # clojure-russia (20)
- # clojure-spec (32)
- # clojure-uk (45)
- # clojurescript (59)
- # community-development (1)
- # core-async (25)
- # cursive (20)
- # datomic (47)
- # emacs (7)
- # fulcro (8)
- # iot (1)
- # iotivity (2)
- # jobs (1)
- # jobs-discuss (8)
- # juxt (11)
- # luminus (5)
- # nrepl (4)
- # off-topic (136)
- # onyx (24)
- # other-lisps (1)
- # parinfer (74)
- # pedestal (1)
- # planck (3)
- # portkey (67)
- # random (1)
- # re-frame (28)
- # reagent (11)
- # reitit (9)
- # remote-jobs (3)
- # ring-swagger (2)
- # rum (3)
- # shadow-cljs (96)
- # slack-help (3)
- # spacemacs (6)
- # tools-deps (3)
- # unrepl (1)
- # vim (4)
What’s the normal way I should be handling speccing functions that have optional arguments?
generally you should use regex ops (like s/cat) to spec args and ops like s/? to handle optional arguments within that
Cool. Thanks 🙂
Is there a way to match an exact number of items in a collection or sequence? Let’s say I want to spec a seq or collection to have 3 and only 3 items in it?
As spec guide states 🙂 …
(s/def ::point (s/tuple double? double? double?))
(s/conform ::point [1.5 2.5 -0.5])
=> [1.5 2.5 -0.5]
https://clojure.org/guides/spec#_collectionss/tuple is good for heterogeneous fixed-size (“slotted”) vectors
s/coll-of with the :count option is good for homogenous (all items match the same pred) collections
like (s/coll-of double? :count 3)
either will work and they conform and gen pretty similarly but I think one or the other usually has a better match on intent
I’m attemping to make a spec for something that can either be any number of vectors, or 0 or 1 strings. currently I have (spc/or :vectortype (spc/* vector?) :stringtype (spc/? string?))
but that doesn’t really seem to be working. (I know this is whacky but, using it to learn the nuances of spec
Anyone see if I am doing something obviously wrong there?
Looks fair to me, what are you getting from that that makes you say it’s not working?
Getting a message in-console saying that the function call does not match the spec when the argument that that spec covers is just a string
Function arguments need to be inside (spc/cat ...)
maybe you’re missing that?
I guess this would have been more useful- here is the whole spec for the function currently (spc/fdef typog :args (spc/cat :props map? :children (spc/or :vect (spc/* vector?) :stringt (spc/? string?))))
so I do have cat. but not sure if im using it right
Do you have the :ret
key in your fdef too? I’ve seen odd results when that was missing.
Hmm I dont, ill try adding that
Everything else looks good to me, with the caveat that I’m still getting comfortable with spec myself, so I might be missing something.
I just threw a :ret any?
in there, but same results sadly. Yeah. It seems to logically make sense to me, and to others I’ve showed it to. Something strange going on perhaps…
I appreciate your help 🙂
the s/or is not a regex op so inserts a new “level” of nesting
so that is always expecting 2 args with the first a map and the second either a collection of 0 or more vector or an empty collection or a collection containing one string
I don’t think that’s what you want
I suspect changing the or
to alt
is probably closer, would at least match the string case you want
but depends what you mean by “0 or 1 strings”
if you mean “no 2nd arg” for “0 strings”, then that case is already covered by (* vector?), which can be 0 vectors
Ahh it was alt! That is what I needed. I did gloss over that in the api but apparently that did not make enough sense for me. haha. Thank you alex.
Also good point about the case of the arg not being there handled by the *, that is definitely true.