Advertisement
Guest User

Untitled

a guest
Feb 13th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Hello,
  2. ;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.
  3.  
  4. ;THE PROBLEM: (def v (safe (/ 10 2)))
  5. ;GIVES: CompilerException java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to
  6. ;clojure.lang.IFn, compiling:(NO_SOURCE_PATH:1:8) , why??
  7.  
  8. ;Sorry if my code might be horrible, im new to this.
  9.  
  10. (defmacro safe [bindings & input]                                              
  11.   (let [
  12.         binds (if (>  (count bindings) 0), bindings nil)                                              
  13.         args  (if binds, input (cons bindings input))
  14.        ]                                      
  15.     (if binds                                                                
  16.     `(let ~binds
  17.        (try ~@args
  18.          (catch Exception e# e#)
  19.          (finally
  20.           (. ~(binds 0) close))))                                      
  21.     `(try ~@args
  22.        (catch Exception e# e#)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement