Create parametric structure with metaprogramming

It seems like your existing code is already almost there–you just need to add T in the appropriate places:

julia> function makestruct(name::Symbol, ndim::Integer)
           dims = (:x, :y, :z)
           fields = [:( $(dims[i])::T ) for i in 1:ndim]
           @eval begin
               struct $(name){T}
                   $(fields...)
               end
           end
       end
makestruct (generic function with 1 method)

julia> makestruct(:bar, 2)

julia> bar(1, 2)
bar{Int64}(1, 2)

1 Like