View difference between Paste ID: jeQ7EcCw and PdT41hZu
SHOW: | | - or go back to the newest paste.
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. 
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-
; THE PROBLEM: (def v (safe (/ 10 2)))
3+
4-
; GIVES: CompilerException java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to
4+
THE PROBLEM: (def v (safe (/ 10 2)))
5-
; clojure.lang.IFn, compiling:(NO_SOURCE_PATH:1:8) , why??
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#)))))