This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-16
Channels
- # announcements (11)
- # beginners (184)
- # calva (91)
- # cider (68)
- # cljdoc (42)
- # cljs-dev (44)
- # clojure (228)
- # clojure-dev (1)
- # clojure-europe (3)
- # clojure-italy (4)
- # clojure-losangeles (6)
- # clojure-nl (9)
- # clojure-spec (73)
- # clojure-uk (19)
- # clojurescript (61)
- # core-async (6)
- # cursive (2)
- # datomic (11)
- # fulcro (28)
- # hyperfiddle (16)
- # leiningen (2)
- # luminus (3)
- # off-topic (19)
- # om-next (1)
- # re-frame (2)
- # reagent (12)
- # reitit (4)
- # ring-swagger (5)
- # shadow-cljs (14)
- # slack-help (6)
- # spacemacs (2)
- # tools-deps (40)
- # vim (15)
- # yada (4)
is there a way to do something like this (s/def ::string-or-number (apply s/or [:string string? :number number?]))
or is it macros to the rescue for e.g. creating a combined spec from a list of regexes?
@stijn apparently (s/def ::string-or-number (s/or #?@(:default [:string string? :number number?])))
also works
that’s gross
but don’t have time to talk about it
When using fdef
, are the arguments validated by :args
supposed to be affected by the destructuring forms present in the function argslist?
eg. in the following, the fact that I am using & [doc]
to read the 2nd arg seems to affect how instrument
works
(defn register-key
[key & [doc]])
(s/def ::register-args (s/cat :key keyword?
:doc (s/? string?)))
(s/fdef register-key
:args ::register-args)
(st/instrument)
(s/valid? ::register-args
[:hello "there"])
;; => true
(register-key :hello "there")
;; => throws
here's an example I have for a similar variadic function:
(defn slarp [path & [blurp? glurf?]]
(str path blurp? glurf?))
(s/fdef slarp
:args (s/cat :path string?
:rest (s/? (s/cat :blurp? int?
:glurf? boolean?))))
(s/exercise-fn `slarp)
=>
([("") ""]
[("" -1 false) "-1false"]
[("e7" 0 true) "e70true"]
[("du9") "du9"]
[("K1" -6 false) "K1-6false"]
[("op0" -3 false) "op0-3false"]
[("2y" 2 false) "2y2false"]
[("qaGWVK") "qaGWVK"]
[("9h1Rh") "9h1Rh"]
[("") ""])
I think your example specs a fixed, 1- or 2-arity function instead of a variadic one that just destructures the first add'l arg
(defn slarp [path & [blurp? glurf?]]
(str path blurp? glurf?))
(s/fdef slarp
:args (s/cat :path string?
:rest (s/? (s/cat :blurp? int?
:glurf? boolean?))))
(st/instrument)
(slarp "a" 1 true)
;; => throws
I can’t say I understand why changing the destructuring form used by the function should affect how the :args
spec works
the difference is that one version is variadic and one isn't, not how you choose to destructure the variadic version
looks like it was recently fixed https://github.com/clojure/clojurescript/commit/4fb83eff87cc456600a3fd21c111e99a41c61285
the actual use case is more like (defn register-key [key & [doc options data])
where both doc
and options
are optional
i’d like to have them show up in argslist (for eldoc etc) but in smaller arities it isn’t known whether the 2nd option would be doc, options, or data
but i just started writing in this way recently and wasn’t sure if it is a great idea
(defn defx [key & [doc data]])
(s/def ::defx-args (s/cat :key keyword?
:doc (s/? string?)
:data map?))
(s/fdef defx
:args ::defx-args)
(st/instrument)
(s/valid? ::defx-args [:a "" {}])
;; => true
(s/valid? ::defx-args [1 1 1])
;; => false
(defx :a "" {})
;; => not thrown (expected)
(defx 1 1 1)
;; => not thrown (but should throw)
now it only validates the length of the arg sequence but doesn’t validate the members
if I rewrite register-key
in multi-arity form, the same spec passes, eg
(defn register-key
([key])
([key doc]))
ie:
(def sort-dates
(comp (map #(update % :date parse-date))
(filter #(nil? (:date %)))
(partial sort-by :date)))
@ben.borders no, but you need to call the ns in which the spec was defined to be able to use it
I want to study the spec
source and understand it. Should I study spec.alpha
or spec-alpha2
?
@jaihindh.reddy The only difference between those right now is the latter has the macro implementations refactored out as functions, that the macros then call.
There are several bugs in spec-alpha2
right now preventing it from actually being used but the code itself is fine to read for learning purposes... although I'm not quite sure what you'll really get from the source, as opposed to the guide and reference material...?
(like many parts of Clojure itself, the source is kinda gnarly and does not represent "typical" Clojure code nor, often, "best practice"... and that's fairly typical of a compiler and its runtime/standard library)
I've recently started to read the Clojure source. Wanted to do that for spec too. Gotta say, it is pretty gnarly.
got a
java.lang.Exception: Unable to resolve spec: :facts.rewards.specs.reward-award/award
FWIW here is the spec:
(s/def ::awards (s/coll-of :facts.rewards.specs.reward-award/award))
the other specs i have defined.. i dont want to enumerate all of them, but they dont require the namespace of the spec they are calling to be explitly required
ex:
(s/def ::qualification
(s/and (s/keys :req-un [::action :facts.rewards.specs.qualification-conditions/conditions])
#(qc/has-completeness-condition? %)))