Fork me on GitHub
#clojure-spec
<
2017-11-21
>
johanatan01:11:47

@hiredman yea, that makes sense. unfortunately wasn't my problem but i figured it out anyway (something unrelated)

johanatan01:11:21

the error message i was getting from spec was unintelligible but i just massaged the code, fixed any issues i saw via manual inspection and it eventually went away

johanatan03:11:16

narrowed it down to instrument actually doing more than I figured it would. looks like it exercises the instrumented with some values that the user itself isn't passing in. this was a bit surprising to say the least

johanatan03:11:36

kept trying to find where the ((nil)) value was coming from in my code/ how that was even possible etc etc

seancorfield03:11:03

@johanatan If you instrument a higher-order function -- one that takes a function as an argument -- then it will use generative testing, as I understand it.

qqq07:11:00

with coll-of, is there a way to say: this collection ca be empty, but it can't be nil i.e. [] is okay, but nil is NOT okay

triss10:11:46

hey all. Is there a spec for something I can use as a predicate for s/valid?

triss10:11:05

I want a spec that matches both predicate functions and the names of specs.

mpenet11:11:46

@qqq it works like this by default

mpenet11:11:07

(s/valid? (s/coll-of any?) nil) returns false

qqq17:11:09

@mpenet: yeah, thanks, I should have run that test, turns out bug was elsewhere and I was misattributing it

johanatan18:11:44

@seancorfield it was a higher-order function in this case. any idea the reasoning behind using generative testing for this case? is it just a 'hack' since the function inputs cannot be inspected to a level of detail necessary for verification?

seancorfield18:11:26

I thought that was explained in the Spec Guide on the web site @johanatan? I certainly wouldn't say it was a 'hack'. It seems perfectly reasonable to me. How else would you verify that a function you're passing as an argument conforms to its spec in this situation?

mpenet18:11:33

It s doable at invoke time

mpenet18:11:51

There is/was a ticket about this

mpenet18:11:29

I personally wished it was like this, now I almost always spec hof as ifn? for instrumentation because of this, which makes it a bit useless

mpenet18:11:31

Imho both make sense in some cases, we should just have the choice

mpenet18:11:03

It was clj-1936 not sure if there is a new ticket started from the discussion there

johanatan19:11:34

@seancorfield it probably is explained but i haven't read the guide from beginning to end so could've missed it. well, yea, in a dynamic language this probably can't be done (except perhaps by inserting metadata onto anonymous functions at their definition sites and then reading it back out at the spec check sites [which now that I mention it, does sound like a better way if it would be possible to impl])

johanatan19:11:49

[but by 'hack' i meant, an unfortunate edge where the abstraction leaks due to limitations of the underlying system being modified]

johanatan19:11:44

@triss isn't spec? what you are asking for?

triss22:11:14

I’m validating on (s/or :fn fn? :keyword keyword? :spec s/spec?) is there a neater way?

qqq22:11:21

I'm starting to love spec's dynamic attributes.

qqq22:11:37

I'm doing some GUI layout, and I can write a spec of :assert that the sum of the width of the elemes of this vector is <= width of the screen

qqq22:11:51

hit is nearly impossible to do in static type systems

seancorfield23:11:25

@triss Do you want to validate that something is a registered spec or that it looks like it could be a spec?

triss23:11:59

is a registered spec would be best for me

seancorfield23:11:17

For the former, you could use #(try (s/get-spec %) true (catch Exception _))

seancorfield23:11:35

That would be false for predicate functions tho'...

seancorfield23:11:55

...but it would validate that you actually had a named, registered spec (keyword).

triss23:11:29

ah i see. that’s a handy snippet fro something no doubt…

seancorfield23:11:53

(so I guess you'd still want (s/or :fn fn? :spec #(try ...)) ?)

triss23:11:01

I’m gonna have to scratch my head for a bit about what I want to match on really… I want anything that woks as the first argument for s/valid?

triss23:11:12

where is the spec for s/valid?

Alex Miller (Clojure team)03:11:25

you can’t actually spec most of the functions in spec without creating an infinite recursion when instrumented

seancorfield23:11:36

Based on the docstring for s/valid?, it doesn't look like there's a spec registered for it.

seancorfield23:11:02

The source shows it calls specize on its spec argument and that in turn is

(defn- specize
  ([s] (c/or (spec? s) (specize* s)))
  ([s form] (c/or (spec? s) (specize* s form))))
at which point you'll have to dig in the source for specize*