This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-04
Channels
- # adventofcode (171)
- # beginners (160)
- # boot (13)
- # chestnut (2)
- # cider (6)
- # cljs-dev (15)
- # cljsjs (35)
- # cljsrn (1)
- # clojure (139)
- # clojure-argentina (3)
- # clojure-brasil (1)
- # clojure-greece (31)
- # clojure-italy (5)
- # clojure-russia (5)
- # clojure-spec (18)
- # clojure-uk (11)
- # clojurescript (42)
- # clojurex (6)
- # core-async (12)
- # cursive (14)
- # dirac (13)
- # emacs (13)
- # events (1)
- # fulcro (46)
- # graphql (7)
- # leiningen (10)
- # lumo (3)
- # mount (31)
- # off-topic (20)
- # onyx (30)
- # perun (4)
- # planck (47)
- # re-frame (28)
- # reagent (14)
- # ring (5)
- # shadow-cljs (3)
- # spacemacs (7)
- # specter (13)
- # timbre (3)
- # unrepl (65)
- # yada (8)
is this actually valid Clojure?
(defn run-query [service query]
(let [{::keys [result error]} (invoke-service service {::query query})]
(or result error)))
just noticed in the clojure spec documentation https://clojure.org/guides/spec
should it not be :clojure
?
maybe it's those namespaced keywrods
well it doesn't compile
1. Caused by java.lang.RuntimeException
Unable to resolve symbol: result in this context
with ::keys
, but it does with :keys
If you’re looking for the :fn
part of an fdef
spec, you can use this:
(s/form (:fn (s/spec `my-fn-name)))
And you can also dig deeper and get the :args part:
(s/form (:args (:fn (s/spec `my-fn-name))))
can the spec from s/keys
be adapted as s/keys*
to avoid repetition?
(s/def :foo/id number?)
(s/def ::foo
(s/keys :req [:foo/id])) ;; <-----------
(defn new-foo [& {:as attr-vals}]
;; add custom attrs
attr-vals)
(s/fdef new-foo
:args (s/cat :attr-vals (s/keys* :req [:foo/id])) ;; <----------
:ret ::foo)
(new-foo :foo/id 1 :foo/bar "baz")
(defmacro spec-keys->spec-keys*
[spec]
(let [form (s/form spec)]
(if-let [[_ & args] (and (seqable? form) (= (first form) `s/keys) form)]
`(s/keys* [email protected])
(throw (ex-info (str "Not a " `s/keys " form")
{:spec spec
:form form})))))