4
u/alexdmiller May 27 '16
I have added a new section to the guide about generators - http://clojure.org/guides/spec#_generators
4
u/jamesconroyfinn May 24 '16
Should the index here be zero or one? Isn't the erroneous card the zeroth element in the hand vector?
(s/explain ::game
{::deck deck
::players [{::name "Kenny Rogers"
::score 100
::hand [[2 :banana]]}]})
At: [::players ::hand 1] val: :banana fails spec: ::card predicate: suit?Why is it [::players ::hand 1]?
4
u/alexdmiller May 24 '16
No, it's the 1th element in the tuple inside the hand vector.
1
u/jamesconroyfinn May 24 '16
Should it be
[::players ::hand 0 1]in that case?I should be able to
get-inwith that seq to get at the invalid data, right? Isn't there a vector around the tuple?3
u/alexdmiller May 24 '16
The path is about the names in the spec, not the keys in the data (there is a lot of overlap between those in this example, admittedly).
2
u/jamesconroyfinn May 24 '16
Okay. So is there a simple way for me to know which card in the hand failed so I can say to a user, for example, the second card you entered was invalid, or would I have to find the invalid data myself?
5
u/alexdmiller May 24 '16
Just to follow up on this, Rich has extended the explain-data to add an :in key that tracks the data keys and that will be in alpha2 (it's in master now).
3
u/jamesconroyfinn May 24 '16
That's great to hear! Can't wait to get Spec into a couple of my projects soon.
Thanks again alexdmiller, richhickey, and the rest of the team!
3
u/alexdmiller May 24 '16
Very similar discussion on this is happening right now in Slack with Rich btw in the #clojure_spec room.
2
3
u/rochea May 25 '16
Wonderful! This solves some of the problems I've had with schema, and I'm planning to try it out this weekend. Any word on CLJS support? I get a lot of value out of sharing schemas between server and client.
1
1
6
May 24 '16 edited May 24 '16
Why is there a separate registry of keywords for maintaining defined specs than clojure.core/def and Vars?
20
u/richhickey May 24 '16
Because enough things are already kept in the reified namespaces, i.e. functions and variables, both of which have to be for performance. The great thing about namespaced keywords/symbols is that we can have many 'databases' if you will, we need not keep everything in the same place.
5
u/weavejester May 24 '16
Currently reified namespaces can be aliased, whereas namespaces that exist just for keywords and symbols have to always be specified in full. Are there any plans for extending
clojure.core/aliasto allow aliasing of keyword/symbol namespaces?3
1
u/fbellomi May 25 '16
Is there a standard way to programmatically "find"/publish specifications for a given ns/var, similar to data_readers.clj for tagged literals? Am I expected to put the specs on the same ns where the entities themselves are defined?
1
u/alexdmiller May 25 '16
As a runtime system, this relies on loading code and registering specs in the registry. You can put specs in the same namespace or in a different namespace - there is no convention. For example, I expect that we will have specs for clojure.core and that they will be in a different ns (not sure what yet).
1
u/peeja May 25 '16
Because enough things are already kept in the reified namespaces, i.e. functions and variables, both of which have to be for performance.
But what's the downside to keeping these in the reified namespaces? It's not like they're too full of functions and variables to accommodate more things.
The great thing about namespaced keywords/symbols is that we can have many 'databases' if you will
I'm not sure what you mean by databases here…do you mean you can use multiple registries, in the same way that multimethods can use custom hierarchies?
we need not keep everything in the same place.
Meaning that putting these on the namespace would require authors to put their specs in the same namespace as the code the specs apply to, and this doesn't?
1
u/alexdmiller May 25 '16
I'm not sure what you mean by databases here…do you mean you can use multiple registries, in the same way that multimethods can use custom hierarchies?
I believe Rich means that namespaced keywords/symbols give us a means of talking unambiguously about specs (or anything else). Specs has a registry (a "database") about specs. Some other lib could have a registry (a "database") about something else.
Meaning that putting these on the namespace would require authors to put their specs in the same namespace as the code the specs apply to, and this doesn't?
No, that it would require us to put more stuff on var meta (the "place"). But we can have more than one place and manage them independently.
3
u/peeja May 25 '16
No, that it would require us to put more stuff on var meta (the "place"). But we can have more than one place and manage them independently.
Oh, he means "place" in the, uh, Rich Hickey sense. :)
2
u/rdpp_boyakasha May 25 '16
clojure.spec seems to address a similar problem space to prismatic/schema. Can someone give us a quick run down of the differences in design goals, and which situations each one is better suited to?
4
u/alexdmiller May 25 '16
I answered a similar question here:
https://www.reddit.com/r/Clojure/comments/4ko47y/introducing_clojurespec/d3hdtyq
2
u/gumvic May 25 '16
As far as ClojureScript is concerned, I wonder, are there really so much platform specific things going on, so it should be "ported"?
E. g. schema works out of the box for both, as far as I know (yes, a different project, but still, a big library doing somewhat similar things).
2
u/alexdmiller May 25 '16
Most of the code will be nearly the same but the parts around instrumentation of vars and things like that differ considerably in how they work in Clojure vs ClojureScript.
2
u/gumvic May 25 '16
I see, thanks.
ClojureScript will definitely benefit from this, when it's ported.
1
u/bearded_jedi May 25 '16
This is amazing! Any idea when it will be production ready (out of alpha/beta)? Outstanding job!
3
1
u/MR_ZORRR May 26 '16
I'm looking forward using spec as validators, like some supercharged assertions. Does that use case seem in-line with the spirit of spec?
1
1
u/spotter May 25 '16
Yay, can't wait to get to my private devbox tonight... at least for Clojure part of the project, since I'm using schema for everything these days (clj|cljc|cljs). Sweet!
8
u/melipone May 24 '16
Can't wait to hear about data generators and testing.