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