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

问题描述

如果我这样做,例如:

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

它失败是因为 reify 看到的是 (qqq) 而不是 (toString [this] "Qqq").

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

通常的解决方案是用我自己的东西包装reify"调用的宏,但它更长且更具侵入性.

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?

期待类似:

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

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

推荐答案

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

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 - 如何让我的宏在系统宏之前展开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:10