With 1.13 Clojure introduces new directives to associative destructuring. Is there a way to get the checked behavior (throw when the key isn't present) on keys destructured without using :keys! like directives ?
I like being able to have vars holding keywords. It allows me to document them and it's also convenient in the case of typos since I tend to catch them sooner in var names rather than in keywords.
For instance consider the follow example :
(def foo
"My nice doc for foo."
::foo)
(def bar
"My nice doc for bar."
::foo)
(def example-map {foo :value})
;; How do I say here that I want foo and bar to be present without a `:keys!` directive ?
(let [{foo-binding foo
bar-binding bar} example-map]
[foo-binding bar-binding])
If there isn't a way, would a :checked directive be a good idea :
;; This would throw, `bar` is absent from `example map`
(let [{:checked {foo-binding foo
bar-binding bar}} example-map]
[foo-binding bar-binding])
A :& directive meaning checked and unbound could also be possible:
;; This would throw, `bar` is absent from `example map` but it wouldn't be bound
(let [{:checked {foo-binding foo}
:& {bar-binding bar}} example-map]
[foo-binding])
Did I miss that feature somehow? If not what do you think about :checked and
:& directives ?
Cheers,
Edit:
I just thought of another use case for a :checked like directive. Directive-less destructuring allows for binding names different from keyword names. We can do :
(let [{id1 :a/id
id2 :b/id} some-map]
...)
Whereas:
(let [{:keys! [:a/id :b/id]} some-map]
...)
does not let you have different bindings for keyword sharing the same name.
Leaving aside the var holding keywords pattern that may be a bad idea. Would the binding of namespace qualified keywords sharing the same name be a valid argument to consider a :checked like directive?