Hi,

I'm looking to make a flexible type which may or may not contain certain 
fields, as well as possibly the addition of fields later on i.e. it's final 
definition is decided at compilation. I figured meta-programming and macros 
might be the way to achieve this as well as be educational for me.

Lets have a toy example:

type MyType
     value1::Int
end

Is the first form of the type, but for another application I might want 

type MyType
    value1::Int
    value2::Bool
    value3::Float
end

Is it possible with a macro or some form of metaprogramming to dynamically 
build up the definition, field by field - perhaps with a loop building a 
code block that is then evaluated?

I started with 

macro PXdefine(x...)

end

where x... could be a varargs I guess like (:Int, :Bool, :Float),
then a loop build those lines along the lines of

for i in 1:length(x)
    :(value$i::$x[i])
end

Then the result might be able to go in between the lines type 'MyType' and 
'end'

But really I'm not 100% sure how to go about building up a chunk of code 
like this to form a more complex whole. The example I've seen in the doc's 
on code generation have evaluations inside a loop to define a function for 
many operators, but I've not seen many examples that build up expressions 
from smaller parts and then evaluate them.

Thanks,
Ben.

Reply via email to