It's not an exported interface, and many of us hope it will change drastically
in the future, but this works:
julia> type MyType
Base.Cartesian.@nexprs 5 i->a_i::Int
end
julia> MyType.names
(:a_1,:a_2,:a_3,:a_4,:a_5)
julia> MyType.types
(Int64,Int64,Int64,Int64,Int64)
--Tim
On Sunday, February 09, 2014 09:17:55 PM Fil Mackay wrote:
> To create a type at runtime, there are a few of methods - each delivering
> an equivalent result:
>
>
> *julia> quote*
> * type Test1*
> * a1::Int32*
> * a2::Int32*
> * a3::Int32*
>
> * end*
> * end*
> quote # none, line 2:
> $(Expr(:type, true, :Test1, quote # line 3:
> a1::Int32 # line 4:
> a2::Int32 # line 5:
> a3::Int32
> end))
> end
>
> *julia> parse("""quote*
> * type Test2*
> * a1::Int32*
> * a2::Int32*
> * a3::Int32*
>
> * end*
> * end""")*
> quote # none, line 2:
> $(Expr(:type, true, :Test2, quote # line 3:
> a1::Int32 # line 4:
> a2::Int32 # line 5:
> a3::Int32
> end))
> end
>
>
> *julia> Expr(:type, false, symbol("Test3"), Expr(:block, [Expr(:(::),
> symbol("a$x"), symbol("Int32")) for x = 1:3]...))*
>
> :($(Expr(:type, false, :Test3, quote
>
> a1::Int32
> a2::Int32
> a3::Int32
> end)))
>
> *Question:* I'm interested if there is a better quote method, something
> that avoids playing with string parsing or explicit Expr()'s with some
> ninja $() magic like pseudo-code:
>
> *quote*
> * type Test4*
> * $for x = 1:3*
> * a$x::Int32*
> * $end*
> * end*
> *end*
>
> Anything similar actually possible?