Ok so I have this macro that is supposed to take a varying number of arguments and then execute them with try and catch. I am assuming that if the argument list arg-list is bigger then 2 then the first element in the list is a binding, like this [a 0] for example. So arg-list may look like this: ([s (FileReader. (File. "text.txt"))] (. s read)).
This is what I’ve come up with:
(defmacro safe [& arg-list] (list 'if (list '< (list 'count arg-list) '2)
(list 'try (list 'eval arg-list) (list 'catch 'Exception 'e 'e))
(list 'do (list 'eval arg-list) (list 'try (list 'eval (list 'rest arg-list)) (list 'catch 'Exception 'e 'e)))))
I have been struggling to get this to work for like two straight days now, but it never works. When I try this macro with for example this:
(safe (+ 2 3))
i get this error:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/eval91 (NO_SOURCE_FILE:100)
I have only been working with Clojure for four days so forgive me if my code is bad.
Well… for a start I suggest you read up on clojure’s macro syntax. I’ll provide a bit of a primmer here but I’m not going to go into depth.
First things first, here’s your macro.
And now for a walk through.
Clojure’s (let) macro demands a vector with an even number of arguments and supports some really interesting behavior called destructuring. For the purposes of this macro, I assume that any valid binding argument will first be a vector and second be of even length. The evaluation of (let) will perform this same check, but this macro must do it as it’s possible that the first form is not a binding but a form to be evaluated and should exhibit different behavior in that case.
As to the macro itself, I use a (let) to process the arguments, the symbol
bindingsserving the double purpose of indicating the presence of bindings as well as taking the binding vector if one is present.formsis re-defined from its initial binding in the arguments (clojure lets you do that) to a value which is impacted by that ofbindingsbeing the entire form sequence which you wish to execute in an error-contained environment. Theexceptsymbol really isn’t called for, it’s just to escape the code duplication of restating that (catch) form in each of the expansion cases.The symbol ` (known as backquote or backtick) which I use is equivalent here to normal quote (‘) except that clojure allows me to use the macro expansion syntax within backquoted forms and not quoted forms. The macro syntax contains the ~ (unquote) operator and the ~@ (insert (unquote)) uperator. Using these three bits of notation I’ve defined both desired cases, the let with a binding form where I insert the binding form and the forms to be tried and the simple try only case.
The conditional could be eliminated to produce
but then you have a superfluous (let) when there is no binding form.