IMHO, using metaprogramming to create variables systematically can be quite handy in some cases. In my work, for example, I have a composite type (say MyType
) that has a bunch of fields (say field1
, field2
, field3
…). An instance of this type is passed to a function like the following:
function foo(myarg::MyType, ...)
field1 = myarg.field1; field2 = myarg.field2 ...
...
end
wait, these statements on the first line seem tedious. So I use a macro @fields2variables
which in turn receives:
function foo(myarg::MyType, ...)
@fields2variables MyType myarg
...
end
and, as long as MyType
is defined, @fields2variables
gets its fieldnames
and transform the code into:
function foo(myarg::MyType, ...)
local field1, field2 ... = myarg.field1, myarg.field2, ...
...
end
where the local
keyword prevents us from incorrectly modifying variables in the enclosing scope.
Another common example in my code is something like:
@inbounds for k = eachindex(a, b, ...)
ak = a[k]; bk = b[k]; ...
...
end
whose first line can also be processed by a macro in a similar way.
I think macros like those are very convenient when writing internal code, but they probably shouldn’t be part of a public API, because they can hide some bugs (although the local
keyword usually helps preventing bugs in the aforementioned examples), so you have to be careful when using them. Of course, the validity this kind of usage is just my opinion.
Again, this does not seem to be the case of the OP’s question, where an Array
or Dict
seems to be more appropriate.