Fork me on GitHub
#clojure-spec
<
2018-04-16
>
hkjels08:04:15

I have a lot of cases in my code where I destructure the input of a function using a spec and then extract the keys using a long vector that’s basically the exact same as the spec itself. Is there a way to just use a spec to destructure keys?

hkjels08:04:59

something like {:keys ::component/params}

hkjels08:04:29

in other languages you have like a splat or explode that would give you all of the keys as vars in the current scope. I get that it would be a bad idea, but with a spec your still explicit

borkdude15:04:26

@hkjels This is exactly what I also asked for recently (some weeks back)

borkdude15:04:53

@seancorfield wrote a macro but I wanted it to do without writing my own let macro

hkjels07:04:50

yeah, this seems like something that should be natively supported

bbss15:04:53

I wrote a macro that uses the registry to return a vector with the keys, and use that in combination with C-c C-v C-w eval and replace in the editor. Of course there is some potential erosion as you'd need to maintain it in multiple places. But at least it's explicit.

mathpunk18:04:18

I'm still working out where to put specs in my project. I'm coming from test-driven development so it seems natural to put fdefs in test namespaces: define what the function should do, separate from the implementation. But then again, does that mean that code that calls the function won't have loaded those specs and will be missing out on the benefits? I'm curious how people are organizing their projects.

bbrinck21:04:53

I tend to put the fdef right before the defn. That provides the most documentation benefits IMO and also anyone who loads my code is guaranteed to get the specs if they want to instrument them

bbrinck21:04:20

As you noted, if you put them in test namespaces, then clients will need to load both namespaces in order to instrument, which I think would be surprising

leongrapenthin18:04:30

@mathpunk use :clojure.spec.test.check/opts

leongrapenthin18:04:45

For some reason they missed to alpha it

mathpunk18:04:33

Ah ha! Thanks so much