This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-07-17
Channels
- # aleph (7)
- # announcements (1)
- # aws (12)
- # beginners (118)
- # calva (15)
- # cider (17)
- # clj-kondo (1)
- # cljdoc (11)
- # cljsrn (1)
- # clojure (108)
- # clojure-dev (32)
- # clojure-europe (3)
- # clojure-italy (4)
- # clojure-kc (1)
- # clojure-nl (4)
- # clojure-portugal (1)
- # clojure-spec (29)
- # clojure-uk (69)
- # clojurescript (91)
- # cursive (14)
- # datomic (8)
- # emacs (13)
- # figwheel-main (12)
- # graphql (4)
- # jackdaw (1)
- # jobs (14)
- # lambdaisland (3)
- # luminus (3)
- # nyc (1)
- # off-topic (14)
- # onyx (23)
- # pedestal (8)
- # re-frame (4)
- # reitit (3)
- # shadow-cljs (6)
- # spacemacs (7)
- # specter (2)
- # tools-deps (3)
- # unrepl (4)
- # vim (7)
- # xtdb (10)
Is there a way to reuse fspec in clojurscript? the only way to spec functions in clojurescript seems to use fdef but that can’t take fspec defined somewhere else as an argument. My goal is to define a function spec in one place and re-use it for all functions that have the same function spec. This can be done in clojure the following way (I believe):
(def n->n-fn-spec (s/fspec :args (s/cat :n number?) :ret number?))
(s/def add5 n->n-fn-spec)
(defn add5 [n] (+ n 5))
(s/def add6 n->n-fn-spec)
(defn add6 [n] (+ n 6))
instead of def
, use s/def
oh, you're trying to reuse the whole fspec as a function's spec
not sure I know enough about the cljs impl to say if this is possible
yes that’s what I’m trying to do. But this example should work in clojure (I think without s/def in the first expression)
it won't work exactly like this in spec 2 in Clojure
but there is a similar path
I tested and it works both with def and s/def in this example (but with s/def it should be ::n->n-fn-spec …)
Looking at clojurescript fdef code it adds the fn-sym to specedvars atom that is used with instrument.
so you can’t use s/def in clojurescript to spec function symbols (at least I can’t get it to work)
the fact that it even works in Clojure is somewhat accidental
being able to use s/def with function spec objects
that won't work in spec 2, but there is a new function s/register that will be available to do the equivalent
that's really incidental - def doesn't do anything with the registry so no change there
the changes are around what s/def does
importantly, an fspec object (not a form)
s/def doesn't work with objects anymore in spec 2, only forms
but s/register is a new function in spec 2 that s/def (macro) uses
I guess my question is, is reusing fspec is a valid use case for spec? or what is the correct way to have reusable function spec?
I can't really answer the latter, but in spec 2 it's possible by registering the same object via s/register. cljs spec 2 work has not been started so tbd
I think they're going to wait until we're much further along with spec 2 before they start working on it
It will be great if with spec 2 things will work the same way in Clojure and Clojurescript
well that's certainly the intent (other than things that can't work the same way)