Fork me on GitHub
#clojure-spec
<
2019-12-08
>
frederic22:12:53

Is this supposed to work in spec 2?

(s/def ::some-keyword ::some-other-keyword)
When I try to validate against ::some-keyword I get this exception:
java.lang.IllegalArgumentException: No implementation of method: :conform* of protocol: #'clojure.spec-alpha2.protocols/Spec found for class: clojure.lang.Keyword
I can fix the symptoms by doing this instead of s/def, but the fact that I have to do a special dance makes me suspect that I might be working against the tool's grain
(s/register ::some-keyword (s/resolve-spec ::some-other-keyword))

frederic22:12:09

I get that spec2 is stricter than spec1 in terms of separating symbolic specs from spec objects. From the documentation (https://github.com/clojure/spec-alpha2/wiki/Differences-from-spec.alpha) (emphasis mine) > s/def is a helpful wrapper macro for s/register that understands how to interpret all kinds of symbolic specs (not just spec forms), including symbols and sets, which will be wrapped in the helper s/spec spec op. I was hoping that ::some-other-keyword would qualify as a symbolic spec, but maybe not.

seancorfield22:12:09

I believe aliased Specs are supposed to work but there are situations where you can forward reference in Spec 1 but you cannot yet forward reference in Spec 2.

seancorfield22:12:31

I assume ::some-other-keyword is defined after ::some-keyword here?

frederic22:12:30

Actually, it's imported from another ns in my real code, shouldn't have tried to simplify that away

seancorfield22:12:36

user=> (require '[clojure.spec-alpha2 :as s])
nil
user=> (s/def ::one pos-int?)
:user/one
user=> (s/def ::two ::one)
:user/two
user=> (s/valid? ::two -1)
false
user=> (s/valid? ::two 1)
true
user=> 

frederic22:12:47

interesting

frederic22:12:54

There must be something else going on then

frederic22:12:42

Let's see if I can build a minimal example that reproduces the behaviour I observe

seancorfield22:12:52

In that simple case, it does let you forward reference FYI -- I tried it in a fresh REPL session with the defs of ::one and ::two swapped and it still worked.

frederic22:12:44

Interesting. Thank you for the handholding 😊, will try to figure out what I'm doing differently and report back

seancorfield22:12:34

With the caveat, again, that there are numerous bugs in Spec2 still at this point. And it sounds like function specs will change dramatically before they're done, according to Alex's latest blog point (and s/and may become non-flowing -- which would be another departure from Spec1). While I was working against the Spec2 branch for a while, things would break between various updates on master, sometimes intentionally but often unintentionally. It's very much a moving target right now.

seancorfield22:12:05

I had hoped to migrate our (large) codebase from Spec1 to Spec2 but, in reality, I think we're "stuck" with Spec1 in our existing code at this point and we'll just adopt Spec2 for new code (depending on how Spec1 and Spec2 interop -- because they don't, right now).

seancorfield22:12:31

Eventually, we'll convert everything over but I suspect we'll do it piecemeal over time.

frederic22:12:00

Mine is just a hobby project, but yes, I might have been over eager there 🙂

frederic23:12:23

Maybe I should just switch back to spec 1, and follow the advice above (<https://clojurians.slack.com/archives/C1B1BB2Q3/p1575541261064300>) to make all my specs :opt rather than :req, merging in :req at the last minute to emulate schema and select.

frederic23:12:59

That way I'd also get orchestra and expound, which would be nice

frederic23:12:59

And probably other libraries and tooling integration that I'm not aware of yet.

eggsyntax23:12:07

> make all my specs :opt rather than :req, merging in :req at the last minute to emulate schema and select. I hadn’t seen the discussion of that above, but that’s pretty much what I’ve come to independently for a large-ish set of domain specs I’ve been working on. Seems to be a decent approach.

eggsyntax23:12:22

Along with a couple of helper functions to eg build a selection from a schema less verbosely by passing in the base schema and the list of attributes that should be required (in the simple cases), and by passing in the base schema and a full tree of requiredness (for complex cases).