null - Is there a quick way to check for nil args in a Clojure function? -
in phil hagelberg's (technomancy) gripes file states following clojure:
nil everywhere , causes bugs difficult find source
now phil smart guy has contributed lot clojure community, , uses stuff - thought worth pondering moment.
one easy way manage nil args function throw error:
(defn myfunc [myarg1] (when (nil? myarg1) (throw (exception. "nil arg myfunc"))) (prn "done!"))
these 2 lines per argument reek of boilerplate. there idiomatic way remove them via metadata or macros?
my question is there quick way check nil args in clojure function?
there clojure language based solution these situations: http://clojure.org/special_forms#toc10
(defn constrained-sqr [x] {:pre [(pos? x)] :post [(> % 16), (< % 225)]} (* x x))
adapted requirements:
(defn constrained-fn [ x] {:pre [(not (nil? x))]} x) (constrained-fn nil) => assertionerror assert failed: (not (nil? x)) ...../constrained-fn (form-init5503436370123861447.clj:1)
and there @fogus contrib library core.contracts, more sophisticated tool
more info on page http://blog.fogus.me/2009/12/21/clojures-pre-and-post/
Comments
Post a Comment