On Jul 13, 2009, at 10:26 PM, [email protected] 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=>

--Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to