Fork me on GitHub
#clojure-spec
<
2016-06-15
>
jimmy04:06:38

hi guys, how do we use variable inside s/keys like this

(def a [::first-name])
(s/def ::user (s/keys :req a))

wotbrew09:06:49

@nxqd: I don't think we can because s/keys is a macro. Though I'm not sure if this is worth it for keys, as it doesn't capture predicates, I would expect it to be a function. As far as I can tell the only thing macro'ey it does is look at forms like (or ::foo ::bar).

Alex Miller (Clojure team)17:06:25

At the moment you can't do this except via eval or something

Alex Miller (Clojure team)17:06:52

But there has been talk of adding a fn entry point that would allow it

seancorfield18:06:09

With check-fn and check-var, is there a way to get a "nice" explanation of the failures, like explain produces?

seancorfield18:06:48

I'm trying to see how the changes in Alpha 6 affect the testing workflow.

eggsyntax19:06:01

@nxqd: (somewhat belatedly) you can replace the var with the function that populates it (it can cache its results, if it's an expensive call), if it's a var that it makes sense to populate at macro eval time.

eggsyntax19:06:47

Unrelated: test.check has a fn (`fmap`) that lets you call an arbitrary fn on the results of a generator. But is there any way to create a generator that just calls an arbitrary fn? I haven't found one.

eggsyntax19:06:45

The only solution I've found so far is to do (fmap (fn [_] do-what-I-want) some-arbitrary-gen), but it's pretty ugly to put in a generator and then ignore what it generates.

jannis20:06:38

Folks, is it a good idea to define fdef function/macro specs next to the actual function/macro definition or would you rather define them in separate my-project.specs kind of namespace?

jannis20:06:40

I'd like to associate most of my functions with specs for automatted random function testing but at the same time I don't want to clutter my code base with specs too much.

jannis20:06:53

I guess there are no best practices established yet?

eggsyntax20:06:47

I've been debating that myself, haven't come to any particular conclusion.

eggsyntax20:06:57

Currently defining them on their own because it's still fairly experimental for us...but as we fully integrate them, I'll definitely consider moving them to live next to what they're speccing.

seancorfield21:06:58

I think we’ll have data specs in separate namespaces. Not sure yet about function specs. Part of me would like them above function definitions — that seemed natural for when we used Schema and core.typed.

seancorfield21:06:45

We went back and forth between core.typed and Schema several times before we abandoned them. With spec being in core, I think we’re more likely to stick with it.

jannis21:06:56

Yeah. It also means you don't have to require the function namespaces and the function spec namespaces when you want to test functions.

seancorfield21:06:31

There the function specs are in a separate clojure.java.jdbc.spec namespace, below the data specs.

seancorfield21:06:56

But that was mostly to ensure pre-1.9 code can still use the library

jannis21:06:46

Here's my first attempt at writing function specs alongside the actual functions: https://github.com/workfloapp/macros/blob/jannis/clojure-spec/src/main/workflo/macros/props.cljc#L9

jannis21:06:44

It would feel a little more natural to define the function spec like`:pre` and :post, e.g.`(defn my-fun [arg1 arg2] {:spec {:args ... :ret ... :fn ...}} <body>)` or something like it, although there are good reasons to keep them separate.

bbrinck23:06:52

@jannis: What do you think about putting the fdefs before the function? IMHO, that's a little clearer. I like having the function specs near the functions since that provides documentation when reading the function. Not sure about data specs, though.

bbrinck23:06:18

or rather, put each fdef before its associated fn

bfabry23:06:09

what have I messed up here...?

Clojure 1.9.0-alpha7
Java HotSpot(TM) 64-Bit Server VM 1.8.0_65-b17
        Exit: Control+D or (exit) or (quit)
    Commands: (user/help)
        Docs: (doc function-name-here)
              (find-doc "part-of-name-here")
Find by Name: (find-name "part-of-name-here")
      Source: (source function-name-here)
     Javadoc: (javadoc java-object-or-class-here)
    Examples from : [clojuredocs or cdoc]
              (user/clojuredocs name-here)
              (user/clojuredocs "ns-here" "name-here")
boot.user=> (require '[clojure.spec :as s])
nil
boot.user=> (require '[clojure.spec.test :as t])
nil
boot.user=> (defn foo [x] (inc x))
#'boot.user/foo
boot.user=> (s/fdef foo :args (s/cat :x integer?) :ret integer?)
boot.user/foo
boot.user=> (t/check-var foo)

java.lang.IllegalArgumentException: No :args spec for boot.user$foo@5c60b94

bfabry23:06:10

boot.user=> (s/fn-spec foo)
nil
boot.user=> (doc foo)
-------------------------
boot.user/foo
([x])
Spec
  args: (cat :x integer?)
  ret: integer?
nil
boot.user=> (s/fn-spec 'foo)