Clojure - 如何使我的宏扩展系统宏之前? [英] Clojure - How to make my macro expand before system macros?

查看:86
本文介绍了Clojure - 如何使我的宏扩展系统宏之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做,例如:

 (defmacro qqq []'(toString [this]Qqq)) 
(reify Object(qqq))

reify 看到(qqq)而不是(toString [this]Qqq)



通常的解决方案是一个宏,用我自己的东西包装reify调用,但它是更长,更具侵扰性。



如何使我的宏变得更强大,使得通常的宏首先被扩展?



期望类似:

 (defmacro ^ {:priority 100500} qqq []'(toString [this]Qqq))
(reify Object(qqq))

 (defmacro qqq []'(toString [this]Qqq))
(expand-first#{qqq}(reify Object(qqq)))


解决方案

强制给定用户宏首先展开的宏(需要 clojure.walk ):

 (defmacro expand-first [the-set& code 
`(do〜@(prewalk
#(if(and(list?%)(contains?the-set(first%)))
(macroexpand-all%)
%)code)))

谁有想法如何使它更好? >

If I do, for example:

(defmacro qqq [] '(toString [this] "Qqq"))
(reify Object (qqq))

it fails because of reify sees (qqq) instead of (toString [this] "Qqq").

The usual solution is a macro that wraps "reify" call with my own thing, but it is longer and more intrusive.

How to make my macros stronger that usual macros to be expanded first?

Expecting something like:

(defmacro ^{:priority 100500} qqq [] '(toString [this] "Qqq"))
(reify Object (qqq))

or

(defmacro qqq [] '(toString [this] "Qqq"))
(expand-first #{qqq} (reify Object (qqq)))

解决方案

The macro that forces given user macros to expand first (requires clojure.walk):

(defmacro expand-first [the-set & code] 
 `(do ~@(prewalk 
  #(if (and (list? %) (contains? the-set (first %)))
  (macroexpand-all %)
  %) code)))

Who has ideas how to make it better?

这篇关于Clojure - 如何使我的宏扩展系统宏之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆