Search code examples
macroscrystal-lang

Crystal lang : in macro, best practice to add an argument to a Calling node


Call#args is of type ArrayLiteral but ArrayLiteral doesn't have any method to insert element. So my problem is how to add an argument into calling elegantly.

I only know this stupid way:

macro method_missing(call)
    Delegator.{{call.name}}("a new parameter", {{call.args}}) {{call.block}}
end

But it's hard to deal with #named_arg and especially #splat_index (although it's member of Block, has the same question here)

Does some better approach exists or it'a todo feature for the compiler developer?


Solution

  • ArrayLiteral can actually be modified and it provides several methods for adding elements: #<<, #[]=, #push, #unshift.

    There is no literal #insert, but it doesn't seem you're explicitly looking for that. It could probably be added to the macro interpreter. But it can also be implemented with #select and #<< for example.