Open
Description
Discussion
Currently, crystal allows modifying all information stored in array/tuple literals in the AST in macros (real literals, not ArrayLiterals obtained from macro methods, those copy their contents).
The current behavior resulting from modifying those literals could be unexpected (and untested?).
I think it would be best to make all arrays already merged into the AST copy on access (or COW?), except const arrays (since those are very often used for storing variable macro state).
Example:
class Foo
def bar(arg) : Array(Int32)
[0]
end
end
puts Foo.new.bar(1) #=> [0]
def change_method
{% Foo.methods.find(&.name.==("bar")).body[0] = 1 %}
end
change_method
puts Foo.new.bar("test") #=> [1]
puts Foo.new.bar(1) #=> [0]
puts Foo.new.bar(1u8) #=> [1]