Clojure

smarter assert (prints locals)

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: Backlog
  • Component/s: None
  • Labels:
  • Approval:
    Vetted

Description

Here is an implementation you can paste into a repl. Feedback wanted:

(defn ^{:private true} local-bindings
  "Produces a map of the names of local bindings to their values."
  [env]
  (let [symbols (map key env)]
    (zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols)))

(defmacro assert
  "Evaluates expr and throws an exception if it does not evaluate to
 logical true."
  {:added "1.0"}
  [x]
  (when *assert*
    (let [bindings (local-bindings &env)]
      `(when-not ~x
         (let [sep# (System/getProperty "line.separator")]
           (throw (AssertionError. (apply str "Assert failed: " (pr-str '~x) sep#
                                          (map (fn [[k# v#]] (str "\t" k# " : " v# sep#)) ~bindings)))))))))

Activity

Aaron Bedra made changes -
Field Original Value New Value
Assignee Stuart Halloway [ stu ]
Reporter Assembla Importer [ importer ]
Priority Major [ 3 ]
Stuart Halloway made changes -
Approval Incomplete
Waiting On stu
Stuart Halloway made changes -
Assignee Stuart Halloway [ stu ]
Waiting On stu richhickey
Christopher Redinger made changes -
Fix Version/s Approved Backlog [ 10034 ]
Fix Version/s Release.Next [ 10038 ]
Andy Fingerhut made changes -
Alex Miller made changes -
Fix Version/s Approved Backlog [ 10034 ]
Fix Version/s Backlog [ 10035 ]
Alex Miller made changes -
Approval Incomplete [ 10006 ] Vetted [ 10003 ]
Alex Miller made changes -
Labels errormsgs

People

Vote (2)
Watch (3)

Dates

  • Created:
    Updated: