2009/7/14 Stephen C. Gilardi <squee...@mac.com>: > > On Jul 13, 2009, at 10:26 PM, sailormoo...@gmail.com wrote: > >> user=> (try (Integer/parseInt "x") (catch Exception _ 0)) >> 0 >> user=> (defmacro parse-int [a default] `(try (Integer/parseInt ~a) >> (catch Except >> ion _ ~default))) >> #'user/parse-int >> user=> (parse-int "x" 0) >> java.lang.Exception: Can't bind qualified name:user/_ (NO_SOURCE_FILE: >> 0) > > You've introduced the name _ to hold the ignored exception. Non-local names > are "resolved" to symbols that include a namespace part: they're interpreted > as references to vars rather than as locals. > > To introduce a local name, you need to use gensym to generate a unique > symbol to name the local. Alternatively, you cause the "# suffix" syntax to > request an "auto-gensym". > > This works: > > user=> (defmacro parse-int [a default] > `(try (Integer/parseInt ~a) > (catch Exception _# ~default))) > #'user/parse-int > user=> (parse-int "123" 4) > 123 > user=> (parse-int "x" 4) > 4 > user=> (macroexpand '(parse-int "123" 4)) > (try (Integer/parseInt "123") > (catch java.lang.Exception ___108__auto__ 4)) > user=>
But, of course, there's no need for a macro here: user=> (defn parse-int [a default] (try (Integer/parseInt a) (catch Exception _ default))) #'user/parse-int user=> (parse-int "x" 0) 0 -- Michael Wood <esiot...@gmail.com> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---