What the argument type is when invoke a macro with a bare literal form args get passed?

Following is a example, i want to know what the type of name is when run the macro getter.

macro getter(name)
  p! {{name.class_name}}

getter unicorns         # => "Call"
getter :unicorns        # => "SymbolLiteral"
getter "unicorns"       # => "StringLiteral"

Following is my questions about getter unicorns.

when unicorns is passed to getter macro as a literal form(i don’t know how to call it), what this unicorns is? why class_name return Call, what is this Call stand for? is a Crystal::Macros::Call ? why we call that name?

thank you.

I guess i know the reason.(probably wrong anyway)

macro getter(name)
  p! {{name}}

getter unicorns

will result in:

In 1.cr:5:8

 5 | getter unicorns
Error: undefined local variable or method 'unicorns' for top-level

unlike a method invoke, when a macro is invoking, the arguments for the macro not be context syntactic analysis until the macro is expanded, name replaced by unicorns, then because unicorns method not exists there, so, fallback to report unicorns method not exists on line 5.

But, if change to this, no error happen.

macro getter(name)
  p! "{{name}}"
  {% debug %}

getter unicorns

because "unicorns" is a valid crystal syntax unit, so, no error.

Maybe my describe not correct anyway, please add new answer for clarify.

Thank you.

Your description is correct. The parser reads getter unicorn as two calls - with parenthesis that’s more visible: getter(unicorn()).

1 Like