When define a macro, specify the macro parameter type, is it possible?

What i want is like this:

macro macro1(arg : Array(String))
end

macro macro1(arg : Hash(String, String))
end

But, compiler complain the syntax error:

Error: expecting token ')', not ':'

So, i am curious, how to write one macro, which make it support both Array or Hash as args?

Thank you.

No, macro parameters can’t have type restrictions. You would have to do it within the macro itself. E.g.

macro test(arg)
  {% if arg.is_a? ArrayLiteral %}
    puts "Array"
  {% elsif arg.is_a? HashLiteral %}
    puts "Hash"
  {% else %}
    puts "Something else"
  {% end %}
end
3 Likes