This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-15
Channels
- # adventofcode (6)
- # beginners (63)
- # boot (39)
- # cider (25)
- # clara (9)
- # cljs-dev (27)
- # clojure (100)
- # clojure-dev (39)
- # clojure-dusseldorf (7)
- # clojure-italy (32)
- # clojure-russia (23)
- # clojure-spec (28)
- # clojure-uk (51)
- # clojurescript (197)
- # core-async (44)
- # cursive (3)
- # datomic (14)
- # emacs (4)
- # fulcro (27)
- # graphql (23)
- # hoplon (4)
- # jobs (9)
- # juxt (1)
- # leiningen (3)
- # nyc (1)
- # off-topic (6)
- # om (7)
- # onyx (6)
- # parinfer (11)
- # re-frame (23)
- # reagent (15)
- # ring-swagger (1)
- # rum (15)
- # shadow-cljs (37)
- # sql (24)
- # uncomplicate (4)
- # unrepl (17)
user> (s/valid? (sorted-set 1 2 3) "you'd think this would return false")
;; throws
ClassCastException java.base/java.lang.Long cannot be cast to clojure.lang.Keyword clojure.lang.Keyword.compareTo (Keyword.java:114)
stack trace shows it is trying to call (s/regex? (sorted-set 1 2 3)), which raises that same exception.
workaround: (s/valid? (hash-set (sorted-set 1 2 3)) ...)
this makes me think the answer will be "you can't use sorted sets as specs"
I looked for a CLJ issue that I thought existed about making set lookup on sorted sets never return an exception because of exceptions thrown by the comparison function, but not sure if I am imagining it.
even if it didn't fail at that point, a sorted set is not a general predicate đ
making a change like that would be weird because what if you supply a buggy comparison function to sorted-set-by
, for instance? it would just swallow them
it could throw them on insert I guess, but would swallow on lookup
Found several related tickets on sorted sets, but not the one I was imagining. My imagination can be vivid.
@andy.fingerhut yeah I know which ticket youâre talking about
it was https://dev.clojure.org/jira/browse/CLJ-1242 which got refocused just on equality, but originally was about equality and lookup
Is there a way to get the keys back from a map spec? i.e. the function get-keys
:
(s/def ::banana (s/keys :req [::length ::type]
:opt [::colour]))
(get-keys ::banana)
=> {:req [::length ::type]
:opt [::colour]}
I can do this by using (s/form ::banana)
but that just gives me a sequence so seems very brittleI don't think there's a better way for now, there's an issue in Jira that seems likely to be what you want but it's still open. https://dev.clojure.org/jira/browse/CLJ-2112
@U053032QC forms have been stable, so (->> (s/form ::banana) rest (apply hash-map))
while waiting the specs for specs.
@U485ZRA58 yes that does look like what I want. @U055NJ5CC yep, it certainly gets me exactly what i want, it just seems a bit wonky
there is likely to be more support for this in the next major update for spec
(let [spec (s/keys :req [:foo/bar])
_ (s/def ::req (s/coll-of keyword?))
keys-spec (s/cat :op symbol?
:args (s/keys* :opt-un [::req]))
{{:keys [req]} :args} (s/conform keys-spec (s/form spec))]
req)
@alexmiller, you always say to "not use conform to coerce values". In this case, we are using conform to "parse" a struct. It's a valid use?
read the topicI donât think Iâve said that. conform is designed to conform values so Iâm not even sure what you mean.
this seems like a pretty ugly workaround (the embedded s/def seems like an obvious place where you could have issues too)
(it's just to fit on let
to easy copy'n'paste on repl)
I think that you say do not use conform to conform values
related to CLJ-2116
do you mean âdo not use conformers to coerce valuesâ which is a very different statement?
that seems wholly unrelated to your example if so
(let [spec (s/keys :req [:foo/bar])
_ (s/def ::req (s/coll-of keyword?))
keys-spec (s/cat :op symbol?
:args (s/keys* :opt-un [::req]))
{{:keys [req]} :args} (s/conform keys-spec (s/form spec))]
req)
@alexmiller, you always say to "not use conform to coerce values". In this case, we are using conform to "parse" a struct. It's a valid use?
read the topicI donât think what youâre doing here is related to coercion
which is independent from whether itâs good :)
my real question is why you want to ask this question in the first place and whether you can arrange things to not ask it
https://clojurians.slack.com/archives/C1B1BB2Q3/p1516035630000041?thread_ts=1516025043.000637&cid=C1B1BB2Q3
half life 3 confirmed
Hi Spec-cers! Which Clojure libraries are using Spec as a protection layer in their APIs, i.e. to validate args? Are they instrumenting API fns âby defaultâ so input args are checked on each call?