On Monday, April 1, 2013 10:26:43 AM UTC-7, Alf wrote:

> I am guessing the problem is that the rethrow macro is expanded and passed 
> to the reader/compiler before the handle-ex macro is. And at that point the 
> compiler sees catch as a "standalone-symbol", not as part of the try 
> special form. Macro-experts, please correct me :)
>
> Tried to quickly catch up with 
> http://www.infoq.com/presentations/Clojure-Macros, but infoq seems slow.
>
> On 1 April 2013 17:21, Bill Robertson <billrob...@gmail.com 
> <javascript:>>wrote:
>
>> I was all excited when I was able to consolidate a lot of try/catch logic 
>> behind a macro earlier this morning. All was good.
>>
>> I felt like I could do a better job of communicating my intentions in the 
>> code though with a rethrow construct rather than writing 
>>     (catch FooException #f (throw #f))
>>
>> I would have liked to have been able to simply write
>>     (rethrow FooException)
>>
>> This failed. Poking around the docs a bit, I see that try/catch is a 
>> special form. Which makes sense.
>>
>> user=> (defmacro rethrow [ex-class] `(catch ~ex-class x# (throw x#)))
>> #'user/rethrow
>> user=> (defmacro handle-ex [message & body] `(try ~@body (rethrow 
>> IllegalArgumentException) (catch Exception x# (throw 
>> (IllegalArgumentException. message)))))
>> #'user/handle-ex
>> user=> (handle-ex "no" (throw (IllegalArgumentException. "yes")))
>> CompilerException java.lang.RuntimeException: Unable to resolve symbol: 
>> catch in this context, compiling:(NO_SOURCE_PATH:1:1)
>>
>> It was a longshot, but I tried to qualify catch. That fails too, because 
>> it's not really there...
>>
>> user=> (defmacro rethrow [ex-class] `(clojure.core/catch ~ex-class x# 
>> (throw x#)))
>> #'user/rethrow
>> user=> (defmacro handle-ex [message & body] `(try ~@body (rethrow 
>> IllegalArgumentException) (catch Exception x# (throw 
>> (IllegalArgumentException. message)))))
>> #'user/handle-ex
>> user=> (handle-ex "no" (throw (IllegalArgumentException. "yes")))
>> CompilerException java.lang.RuntimeException: No such var: 
>> clojure.core/catch, compiling:(NO_SOURCE_PATH:1:1)
>>
>> Is this possible to do within the normal bounds of the language?
>>
>> Thanks!
>>
>>
>>  -- 
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@googlegroups.com<javascript:>
>> Note that posts from new members are moderated - please be patient with 
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+u...@googlegroups.com <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+u...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>  
>>  
>>
>
>
That is the opposite of the problem: `try` searches for a `catch` without 
macroexpanding its body forms. You could come at the problem differently, 
by writing a macro that expands to an entire try+catch 
form: (try-rethrowing [IllegalArgumentException] (do a b c) (catch 
Exception e e) (finally (foo))) is a macro that you could successfully 
write.

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to