Required keys for destruction
Is there a way to get an exception if the keys you are trying to destroy are not in the map passed to your function? Would this be a good use case for a macro?
For example:
(defn x [{:keys [a b]}] (println a b))
I would like this to work:
(x {:a 1 :b 2})
But this is to throw an exception (: b is missing)
(x {:a 1})
+3
source to share
1 answer
How about a precondition ?
(defn x [{:keys [a b]}]
{:pre [(some? a) (some? b)]}
(println a b))
user=> (x {:a 1})
AssertionError Assert failed: (some? b) user/x (NO_SOURCE_FILE:1)
Edit: Yes, you can use a macro to handle this for you. Maybe something like this:
(defmacro defnkeys [name bindings & body]
`(defn ~name [{:keys ~bindings}]
{:pre ~(vec (map #(list 'some? %) bindings))}
~@body))
(defnkeys foo [a b]
(println a b))
(foo {:a 1 :b 2})
(foo {:a 1}) ;; AssertionError Assert failed: (some? b)
(foo {:a 1 :b nil}) ;; AssertionError Assert failed: (some? b)
+4
source to share