Guest User

Untitled

a guest
Feb 13th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; This little macro should act as try-catch clause. It takes an arbitrary number of args and executes them   ; in a try-catch. If there are any bindings to something ex a filestream. It should be able to use it in the ; try clasue and then finally close it.
  2.  
  3. ; THE PROBLEM: (def v (safe (/ 10 2)))
  4. ; GIVES: CompilerException java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to
  5. ; clojure.lang.IFn, compiling:(NO_SOURCE_PATH:1:8) , why??
  6.  
  7. (defmacro safe [bindings & input]                                              
  8.   (let [
  9.         binds (if (>  (count bindings) 0), bindings nil)                                              
  10.         args  (if binds, input (cons bindings input))
  11.        ]                                      
  12.     (if binds                                                                
  13.     `(let ~binds
  14.        (try ~@args
  15.          (catch Exception e# e#)
  16.          (finally
  17.           (. ~(binds 0) close))))                                      
  18.     `(try ~@args
  19.        (catch Exception e# e#)))))
Advertisement
Add Comment
Please, Sign In to add comment