Fork me on GitHub
#clojure-spec
<
2018-12-21
>
ag00:12:53

is it possible to run count of (s/def #{"a" "b" "c"}) ?

taylor01:12:38

is that s/def missing a name?

taylor01:12:56

e.g. (s/def ::foo #{1 2 3})

taylor02:12:01

anyway if your spec is a literal set, you could do

(s/def ::foo #{1 2 3})
(s/form ::foo)
=> #{1 3 2}

taylor02:12:25

or you could define the set separately, and reference it from the s/def and wherever you need to count it

ag00:12:10

how can I find out how many elements there?

ro601:12:35

Is there a way to abstract over whether a key in a map is namespaced or not? In a certain context, as long as the value conforms, I want to accept both with the same spec.

Alex Miller (Clojure team)02:12:29

If you’re using s/keys, the :req-un and :opt-un will do that

urzds17:12:21

Can I spec record methods just like any ordinary function?

urzds17:12:57

The guide does not really go into details on records, except that one can spec their fields/attributes: https://clojure.org/guides/spec

Alex Miller (Clojure team)17:12:18

records don’t really have methods

Alex Miller (Clojure team)17:12:33

they implement interfaces/protocols which define methods

Alex Miller (Clojure team)17:12:47

currently, you can’t spec protocols or interface methods

Alex Miller (Clojure team)17:12:27

and because of the calling implementation it’s not possible to instrument them (and probably not something we’re ever going to really do because of that)

Alex Miller (Clojure team)17:12:52

but I wouldn’t completely rule it out

urzds17:12:25

@alexmiller So I can spec neither the protocol for all its implementations, nor every record implementation individually?

urzds17:12:28

But I could place the implementation in a function, which I could spec, and then from the record implementation just call that function? I.e. insert one step of indirection?

Alex Miller (Clojure team)17:12:41

yes, but I wouldn’t do that just to be able to spec it

urzds17:12:46

Well, right now my implementations look like (s/assert ...args...) (s/assert ...body...), which is hardly better...

Alex Miller (Clojure team)17:12:06

this is a case where the impl of protocols (designed to tap into the highly optimized java/jvm calling semantics) is at odds with the dynamic indirection possible in Clojure via vars

Alex Miller (Clojure team)17:12:04

I guess maybe there’s some possible future where newer Java capabilities like method handles could be used to implement this kind of thing

Alex Miller (Clojure team)17:12:48

retaining most of the speed but also giving you the dev time instrumentation

urzds17:12:53

So what do you do when during development you want to ensure that the maps returned from a method implementation of the record conform to a spec? And that all callers call it properly? Insert (s/assert arg-spec arg) (s/assert ...body...), like I did?

urzds17:12:36

I don't really care if the call slows down by an order of magnitude, if it helps me in debugging where that borked data came from.

urzds17:12:12

Or do you just split up the code in such small pieces that the method implementation in the record does not really do any significant work anymore, so you have enough functions that you can properly instrument?

Alex Miller (Clojure team)17:12:08

I don’t think there is one canonical answer to that question, it depends on the code

Alex Miller (Clojure team)17:12:03

splitting up code into smaller pieces is usually a good idea though

urzds17:12:55

Thanks, I'll start with that then, maybe it already alleviates the problem.

dominicm22:12:45

Is there any work yet on determining whether a spec is a subset or a superset of another? I think Rich has hinted at this a lot.

ro623:12:43

is anyone using (s/cat ...) to spec strings? I realize I could just delegate to a traditional string regex with (re-matches ...), but then I give up on high-fidelity explain results no?