clojure-spec

liebs 2023-02-01T17:59:35.476519Z

question about resolving specs: is there a way to require a spec out of the global registry as if it were a regular var in a ns? I have discovered that I can't do something like (require [foospec.bar :refer [baz]]) presumably because baz is not interned in the foospec.bar ns but is instead in the registry? Is that correct?

Alex Miller (Clojure team) 2023-02-01T18:13:37.968129Z

correct, specs are not vars

Alex Miller (Clojure team) 2023-02-01T18:14:15.431449Z

you could put a spec in a var with (def my-spec (s/get-spec ::foo)) if you wanted to for some reason

liebs 2023-02-01T18:15:28.000869Z

no, not necessary, I'm fine with it being in the registry. So if require the whole foospec.bar ns say as fspec and then resolve ::fspec/baz its just looking in the registry for that fully qualified name?

liebs 2023-02-01T18:16:16.953809Z

that's how I have been doing it at any rate

Alex Miller (Clojure team) 2023-02-01T18:16:40.940459Z

not sure I understand "require the whole foospec.bar ns say as fspec " but fspecs are also just specs in the registry (but their keys are fully-qualified symbols, not keywords)

Alex Miller (Clojure team) 2023-02-01T18:18:25.937819Z

s/fdef just creates a spec with s/fspec, then adds it to the registry (which is just a map) with the fq symbol as the key

liebs 2023-02-01T18:18:31.467179Z

sorry, that was a poor choice of alias on my part

liebs 2023-02-01T18:18:47.070629Z

I am not talking about function specs, just any spec in general

Alex Miller (Clojure team) 2023-02-01T18:19:07.244909Z

then yes, that is how it works

👍🏻 1
seancorfield 2023-02-01T20:16:52.269709Z

@bhlieberman93 Don't forget there's :as-alias now in a require so you can create an alias for resolving Spec names without having to actually load a real namespace.

seancorfield 2023-02-01T20:18:17.273019Z

(ns using.specs
  (:require [i.do-not.exist :as-alias idne]))

::idne/bar ;=> i.do-not.exist/bar
and no i/do_not/exist.clj file is needed for this.

liebs 2023-02-01T23:35:56.603039Z

oh thanks @seancorfield I did not know about as-alias. That's helpful.