1
votes

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.

2

2 Answers

4
votes

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.

(defmacro safe [bindings? & forms]                                              
  (let [bindings (if (and (even? (count bindings?)) (vector? bindings?))        
                   bindings? nil)                                               
        forms    (if bindings forms (cons bindings? forms))                     
        except  `(catch Exception e# e#)]                                       
    (if bindings                                                                
    `(let ~bindings (try ~@forms ~except))                                      
    `(try ~@forms ~except))))

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 bindings serving the double purpose of indicating the presence of bindings as well as taking the binding vector if one is present. forms is re-defined from its initial binding in the arguments (clojure lets you do that) to a value which is impacted by that of bindings being the entire form sequence which you wish to execute in an error-contained environment. The except symbol 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

(defmacro safe [bindings? & forms]                                              
  (let [bindings (if (and (even? (count bindings?)) (vector? bindings?))        
                   bindings? [])                                               
        forms    (if-not (empty? bindings) 
                   forms (cons bindings? forms))                     
        except  `(catch Exception e# e#)]                                      
    `(let ~bindings (try ~@forms ~except))))

but then you have a superfluous (let) when there is no binding form.

1
votes

You don't need eval for this - the results of macro-expansion are already eval'ed. What you want is most easily accomplished using syntax-quoting inside your macro:

(defmacro safe [& args]
  (if (< (count args) 2)
    `(try ~@args (catch Exception e# e#))
    `(let ~(first args)
       (try ~@(rest args) (catch Exception e# e#)))))

(safe (+ 2 3)) => 5
(safe [x 3] (+ 2 x)) => 5
(safe (Integer/parseInt "a")) => #<NumberFormatException java.lang.NumberFormatException: For input string: "a">

The reason for the exception you're seeing is that arg-list in your example will be a list of forms, which in your case has a single item that is the list '(+ 2 5). When you eval a list whose first item is a list, then the inner form is eval'ed first and then the outer form is eval'ed:

(eval '(+ 2 3)) => 5
(eval '((+ 2 3))) => (eval '(5)) => exception, because 5 is not a function.

Your macro might be fixed by changing (list 'eval arg-list) to (list 'eval (first arg-list)).