Fork me on GitHub
#clojure-spec
<
2018-06-26
>
athos02:06:43

@blance I was working on a library for managing generators separately from spec definitions a couple of months ago. If you’re interested, take a look at it! https://github.com/athos/genman

👍 4
blance02:06:47

this looks promising! i'll definitely give it a try next time I write spec

😉 4
justinlee21:06:18

question about spec-tools.data-spec. it seems like with data-spec there are now two namespaces: the one in the spec registry and the symbol i define in my own code. this makes sense with the one example in the readme because it is a map. but what about for code like

(def hit-box-spec
  (ds/spec ::hit-box [number?]))
It seems like both ::hit-box and hit-box-spec are now symbols that refer to a spec. I’m just getting a bit confused about how to use this library with non-map specs.

noisesmith21:06:56

::hit-box is a keyword, specs resolve via a central registry that uses keywords. hit-box-spec is a var, containing whatever ds/spec returns (not necessarily the same value stored under that key in the spec registry, but maybe?)

justinlee21:06:41

yea. that’s how i understand it. i think i’m just confused why ds/spec doesn’t rely on the fact that it creates a side effect in the spec registry. i’m not sure why i’d want to also have a var in my namespace that points to a value that is apparently interchangeable (at least it is when using s/valid?

noisesmith21:06:27

but it also points to the literal data,that ds/ functions can use to create other specs, right?

noisesmith21:06:32

I'd assume that's the reason

justinlee21:06:57

I don’t think so. In the readme, they use a different var to point to the data used to create the spec. So they might write the above as (def hit-box-spec (ds/spec ::hit-box hit-box))

justinlee21:06:24

but it might have to do with these transformations, which I’m not using and don’t understand. that would make sense

justinlee22:06:18

hm what it is doing is more complex than this. my statement above that ::hit-box and hit-box-spec are interchangeable for valid? is wrong

ikitommi22:06:11

@lee.justin.m side-effects are bad, I think the data-specs could benefit from a local-keys variant that doesn’t need to register the keys. data-specs are anyway not in align to the Spec filosophy of reusable keys, so why not go further down the road…

ikitommi22:06:26

but for the original:

(ds/spec ::hit-box [number?])
can be written:
(ds/spec
  {:name ::hit-box
   :spec [number?]})
… and as there are no keys, the :name can be omitted:
(ds/spec
  {:spec [number?]})

ikitommi22:06:02

to register that, you can say:

(s/def ::hit-box
  (ds/spec
    {:spec [number?]}))

justinlee22:06:07

@U055NJ5CC ah i see. i should be using the map syntax.

justinlee22:06:20

as for whether side effects are bad, that’s just the way spec works, right?

justinlee22:06:31

you have to register your spec with the registry

justinlee23:06:09

the thing i’m not getting right now is this:

(def banana-spec
  (ds/spec ::banana {:id integer?
                     :name string?}))
(st/registry #".*banana.*"))
=> (:seekeasy.app-state$banana/name :seekeasy.app-state$banana/id)

justinlee23:06:40

i see that the two subspec are registered, but why not the main banana spec?

ikitommi23:06:56

if you try to put a map somewhere in the data-spec and don’t provide a :name, it will fail-fast:

(ds/spec
  {:spec [[[[[[[{:a int?}]]]]]]]})
; CompilerException java.lang.AssertionError: Assert failed: spec must have a qualified name
; (qualified-keyword? n), compiling:(data_spec_test.cljc:380:3)

ikitommi23:06:21

good question, not sure, maybe it should?

justinlee23:06:34

i just don’t even know how it works 🙂

justinlee23:06:11

basically i was expecting an unmunged :seekeasy.app-state/banana given that i provided it that as the name of the spec. although i guess that’s consistent with the readme, now that i think about it. the only thing that got registered were the subspecs.

ikitommi23:06:43

if there would be the local-keys, there would be no registering of any specs. that in mind, having a function called spec doing registration of the top-level would be bit odd.

ikitommi23:06:17

there could be ds/def for that…

justinlee23:06:30

oh i see what you mean

ikitommi23:06:41

… but (s/def ::name (ds/spec ..)) is the way to do it now.

justinlee23:06:59

because spec does (s/def ...) so we would expect it to cause a side effect

justinlee23:06:20

but here it’s just a function so we need to def it ourselves. okay. this is coming together for me.

justinlee23:06:47

I guess I thought there was a separate data structure for the specs. But they are just stored on normal vars?

justinlee23:06:11

the existence of the st/registry made me think this

ikitommi23:06:41

all Specs are just values, you can store them in a Var.

ikitommi23:06:59

(s/valid? string? "1")

ikitommi23:06:34

(s/valid? (ds/spec {:spec [int?]}) [1 2 3])

ikitommi23:06:23

.. unless you register them and get a name than can be used in s/keys. I think it’s the only one that requires a spec to be registered?

justinlee23:06:56

damn that is confusing 🙂

ikitommi23:06:29

(s/valid? (s/coll-of int? :into []) [1 2 3])

justinlee23:06:32

basically, the thing that confused me is that if you do (s/def ::something ...) that will show up in the registry even it if isn’t meant to be used as a key but if you do (def something (ds/spec ...)) it doesn’t show up. but both work. i hadn’t appreciated the fact that nothing cares about the registry except for s/keys

justinlee23:06:38

btw, thanks for spec tools. i really really really prefer the self-documenting format of schema, and now i have my cake and eat it too

ikitommi23:06:52

thanks! it’s kind of a roque lib, I hope the final version of spec will make much of it redundant.

ackerleytng23:06:15

what types of functions do you guys normally spec? do you spec utility functions?

justinlee23:06:14

the answer you’ll hear most commonly is “at data boundaries” or something like that. e.g. reading data from a file or network or moving from one chunk of code to another, rather than doing it wholesale on every internal function

noisesmith23:06:22

I'd put it as "system boundaries" rather than "data boundaries" but yes, exactly that

noisesmith23:06:49

where system boundaries are has a lot to do with your design, but if your system is designed it should have some :D

ackerleytng23:06:23

won't your system boundaries keep changing?

ackerleytng23:06:38

as you compose functions with functions

noisesmith23:06:06

if your system boundaries are changing those weren't your system boundaries

ackerleytng23:06:13

another question! how do you define an fspec where you don't care about the name of the argument, but you care about the type?

ackerleytng23:06:41

ah thanks that sounds right haha

noisesmith23:06:42

the idea is that it isn't a system if you don't define some limit or border, that's really the first step. What the boundaries are, and which things cross them, should be one of the first things defined, and often you'll want to define things so it changes relatively rarely

noisesmith23:06:27

@ackerleytng for the names of things in specs, the reason to have the names is for the error messages you get without a match. Otherwise the output of a failure turns into a soup of data types that isn't very helpful.

ackerleytng23:06:52

oh! so it doesn't actually match against the name of the actual parameter?

noisesmith23:06:02

if it is the thing I'm thinking of it's a series of name / type for each arg right?

noisesmith23:06:23

the name is used in generating the message, it doesn't have to match the arglist or anything