有没有办法查看 Crystal 宏扩展到什么?

Is there a way to see what a Crystal macro expands to?

我有一个宏拒绝按预期工作,我想知道是否有办法查看它扩展到什么,Crystal 中的 lisp 是否有类似 macroexpand-1 的东西?如果是这样,我该如何使用它?谢谢!

{% debug %} 放在宏的末尾将在编译时打印它的内容。

例如

macro foo
  ...
  {% debug %}
end

出现另一个工具是 crystal tool expand

ex 文件 mapping_test.cr

require "json"
require "./json_mapping"# wget https://raw.githubusercontent.com/crystal-lang/json_mapping.cr/master/src/json_mapping.cr
class Location
  JSON.mapping(  # <---- line: 4, column: 3
    lat: Float64,
    lng: Float64,
  )
end

运行 像这样:

crystal tool expand -c mapping_test.cr:4:3 mapping_test.cr(必须在正确的位置或至少在宏内,否则将得到“未找到扩展”

产出

1 expansion found
expansion 1:
   JSON.mapping(lat: Float64, lng: Float64)

# expand macro 'JSON.mapping' (/home/a/json_mapping.cr:236:3)
~> ::JSON.mapping({lat: {type: Float64, key_id: lat}, lng: {type: Float64, key_id: lng}})

...

ref https://groups.google.com/g/crystal-lang/c/L7ADzhRQGLk