try* macro to catch multiple exception classes with one body. feedback is needed.

瀏覽次數:361 次
跳到第一則未讀訊息

Max Gonzih

未讀,
2013年6月19日 上午11:00:172013/6/19
收件者:clo...@googlegroups.com
Hi, I implemented small macro to catch multiple exception classes with one body.

https://gist.github.com/Gonzih/5814945

What do you think? Are there better ways to achieve similar results?

Thanks!

Meikel Brandmeyer (kotarak)

未讀,
2013年6月20日 凌晨1:47:372013/6/20
收件者:clo...@googlegroups.com
Hi,
I would just extend try a simply as possible: simply add the catch-all, but keep catch and finally as is. Here my try:

    (defmacro try*
      [& body]
      (let [catch-all?   #(and (seq? %) (= (first %) 'catch-all))
            expand-catch (fn [[_catch-all exceptions & catch-tail]]
                           (map #(list* 'catch % catch-tail) exceptions))
            transform    (fn [form]
                           (if (catch-all? form)
                             (expand-catch form)
                             [form]))]
        (cons `try (mapcat transform body))))

    (try*
      (println :a)
      (println :b)
      (catch-all [A B C] e (println (type e)))
      (catch D _ (println "Got D!"))
      (finally (println "Finally!")))

expands to

    (try
      (println :a)
      (println :b)
      (catch A e (println (type e)))
      (catch B e (println (type e)))
      (catch C e (println (type e)))
      (catch D _ (println "Got D!"))
      (finally (println "Finally!")))

Kind regards
Meikel

Max Gonzih

未讀,
2013年6月20日 凌晨2:57:572013/6/20
收件者:clo...@googlegroups.com

Right, simpler solution, thanks.

Regards,
Max

--
--
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
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
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/0Suarc57WCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Max Gonzih

未讀,
2013年6月20日 凌晨3:25:122013/6/20
收件者:clo...@googlegroups.com
I updated my macro to your solution, looks really simple and works like before. I don't know why I overcomplicated my original solution so much :).

Thanks again!

Herwig Hochleitner

未讀,
2013年6月20日 下午4:26:132013/6/20
收件者:clo...@googlegroups.com
One thing to consider: try* is a compiler builtin form. Those are currently not even namespaced. That might lead to confusion for readers.


2013/6/20 Max Gonzih <gon...@gmail.com>
--
--
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
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
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.

Max Gonzih

未讀,
2013年6月22日 上午11:42:202013/6/22
收件者:clo...@googlegroups.com

I can't think about another name for my macro :). Any ideas?

Regards,
Max

You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/0Suarc57WCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
回覆所有人
回覆作者
轉寄
0 則新訊息