This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-14
Channels
- # beginners (183)
- # boot (6)
- # cider (106)
- # cljs-dev (17)
- # cljsjs (2)
- # cljsrn (2)
- # clojure (56)
- # clojure-italy (14)
- # clojure-nl (39)
- # clojure-spec (49)
- # clojure-uk (138)
- # clojurescript (197)
- # core-logic (37)
- # cursive (22)
- # datascript (5)
- # datomic (29)
- # devcards (18)
- # emacs (1)
- # events (8)
- # figwheel (1)
- # fulcro (59)
- # lein-figwheel (1)
- # leiningen (1)
- # off-topic (54)
- # onyx (3)
- # pedestal (1)
- # portkey (4)
- # re-frame (18)
- # reagent (5)
- # reitit (43)
- # ring (6)
- # ring-swagger (26)
- # shadow-cljs (42)
- # spacemacs (8)
- # specter (12)
- # sql (3)
- # tools-deps (21)
- # vim (18)
what’s the way out here, alter-var-root? https://github.com/bhb/expound/issues/19
set! will only work on a dynamic var if you are in the dynamic scope of a binding call
Repls like clojure.main establish this
So you either need to use binding or modify the root value with alter-var-root
You could override the print-method for String, although that might have some adverse consequences
probably not a good idea 🙂 the reason I ask is that a spec error (with expound) flooded my emacs buffer so bad, I had to forcequit it…
@borkdude although it elides potentially useful information, you can configure expound to not print out “failing spec” info, which would likely shorten the output considerably.
Is there an idiomatic way to check if a spec exists?
i.e. given a namespaced keyword how to check if a spec is registered for it
you could nope use (s/form ::spec)
and it returns :clojure.spec.alpha/unknown
s/get-spec
which returns nil for unknown specs
@taylor I’m getting a compiler exception for an unknown spec instead of a keyword
Ok. Maybe I’ll just implement with try catch
Oh d’oh
Perfect. Thanks lol
I didn't know this existed either :man-shrugging: never had to look up a spec like that
(defn registered-spec? [x]
(and (qualified-keyword? x)
(some? (s/get-spec x))))
Yeah. Working on something where a macro may receive a namespaced keyword with an associated spec…so hopefully this all works out at compile time. Unfortunately have to do actual work now. Thanks a lot for your help…was going to hack something in but I feel way better with thisgiven that macro expansion is a simplification step before making bytecode, you either end up with a much more complex compiler or taking the huge performance hit of having an interpreted language rather than a compiled one
(in order to have first class macros)
@noisesmith i'm not arguing for first-class macros (although that would be nice). i'm arguing for not using second-class macros in libraries
oh, I misinterpreted you then
Can I generate specs en masse? Something like:
(doseq [k my-keys]
(s/def k (s/double-in :infinite? false :NaN? false :min 0)))
This was my gut feeling of what was going on. Any way to "force" it to evaluate the keyword? At this point I can see the value of writing them out by hand, but still curious...
I have done:
(doseq [[spec-kw field-spec desc] fields]
(eval `(s/def ~spec-kw ~(build-internal-spec field-spec))))
I don’t even feel bad about it.
yeah you could use a macro to spit out a bunch of s/def
's for your keywords:
(defmacro alias-kws [ks]
(let [defs (map #(list 's/def % ::my-spec) ks)]
`(do ~@defs)))
@donaldball interesting...using eval
does do exactly what I'm after. It's a bit of a hack, but it beats having to repeat these massive lists of keywords over and over again...
It would be nicer if spec had an explicit affordance for this use case, but I don’t really think it’s abusive to take advantage of the fact that clojure is a lisp 😛 . I wouldn’t reach for this often, mind, but in my case I had a gigantor table of fields for a structured file and this lets me leverage an edn representation for spec validation as well as actually e.g. generating serializers
Very cool, I agree that there are definitely valid use cases. Not to mention that the prospect of generating specifications from concretions is extremely interesting in its own right :thinking_face: Appreciate the help (@taylor as well, thank you)
@cjsauer most (all?) attempts to meta-program spec will fail-- it's apparently intended to be hand-written in the first-order only.
Ooph...ok, thanks @johanatan
@johanatan as noted, spec is currently built for human written code/input. There are plans for a more programmatic API which will open up meta-programming. I think building the latter first would have led to a lot more breaking changes (since folks would have built code against the API). Make sense?
@donaldball interesting...using eval
does do exactly what I'm after. It's a bit of a hack, but it beats having to repeat these massive lists of keywords over and over again...