metaprogramming - Dealing with end keyword in macros for array indices -
suppose have array of composite type follows:
type mytype a::int b::float end myarray=mytype[]
for obvious reasons, able use simple indexing access fields of composite types follows:
avals=myarray[1:3].a
the following macro can accomplish type of indexing, long have numeric iterable array:
macro getarray(exp) iter=eval(exp.args[1].args[2]) exp.args[1].args[2]=:i; :[$(esc(exp)) $(esc(:i)) in $iter] end
how can write similar macro capable of dealing array indices end
keyword, i.e.:
avals=@getarray myarray[1:end].a
the following macro solves not indexing problem sets correct output type:
macro getarray(exp) quote ftype=typeof($(esc(exp.args[1]))[1].($(esc(exp.args[2])))); ftype[item.($(exp.args[2])) item in $(esc(exp.args[1]))] end end
Comments
Post a Comment