【问题标题】:Why doesn't my macro give any output为什么我的宏没有给出任何输出
【发布时间】:2013-06-12 17:37:16
【问题描述】:

我编写了一个简单的宏,用于在调试时打印出表达式及其结果。

(defmacro dbg-print 
  "Print out values or expressions in context"
  [& rest]
  `(let [symb-str# (map str '~rest)
         symb-evl# (list ~@rest)
         pairs# (map #(str %1 %2 %3 %4) symb-str# (repeat ":") symb-evl# (repeat " "))
         str# (reduce str pairs#)]
     (printf "%s\n" str#)))

它是这样工作的:

(defn my-func1
  [arg]
  (dbg-print (+ arg 1)))
(my-func1 1)

输出结果

(+ arg 1):2 

然后我遇到了一个问题,我从索引中读取了一些内容:

(defn my-func2
  [first & rest]
  (dbg-print rest)
  (nth rest 1))
(my-func2 1 2)

这根本不提供任何输出。 dbg-print 宏下方的错误如何阻止其打印?我还注意到,如果我在宏中使用 println 和 format 而不是 printf,错误就会消失。但我还是想知道发生了什么。

【问题讨论】:

    标签: macros clojure


    【解决方案1】:

    这不是宏问题,而是 I/O 问题。在刷新 stdio 之前遇到异常。

    printf 使用 print,它使用 pr 而没有刷新。

    println 使用 prn,它使用 pr 但在 *flush-on-newline*true 时刷新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-15
      • 2022-01-11
      • 2013-11-24
      • 2019-05-14
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      相关资源
      最近更新 更多