简体   繁体   中英

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

I've a macro that refuses to work as expected and I was wondering if there was a way to see what it expands to, is there something like macroexpand-1 from lisp in Crystal? If so, how do I use it? Thanks!

Placing {% debug %} at the end of the macro will print it's contents at compile time.

eg

macro foo
  ...
  {% debug %}
end

Appears another tool is crystal tool expand

ex file 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

run it like this:

crystal tool expand -c mapping_test.cr:4:3 mapping_test.cr (must be right location or will get 1no expansion found 1)

output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM