Fork me on GitHub

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?


something like {:keys ::component/params}


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


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


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


yeah, this seems like something that should be natively supported


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.


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.


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


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


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


For some reason they missed to alpha it


Ah ha! Thanks so much