I think the Parameters.jl [1] package could do what you're looking for.
It provides keyword constructors for types (with or without defaults):
@with_kw immutable My{R}
a::R = 5
b::Int # no default
end
My(b=4)
My(a=1
b=2)
My() # errors because b has no default
# there is also a copy and modify constructor:
m1 = My(b=4)
My(m1, a=2) # same as m1 but with a=2
# it also comes with some convenience macros to use your
# parameter-structures inside a function
function foo(x, y, para::My)
@unpack_My para # this will expand to a,b = para.a,para.b
x + y + a
end
Note that keywords are slow, so if your constructing types in hot inner
loops, then Parameters.jl is not suitable.
Also, I got a (unregistered) mesh library, LMesh.jl [2], maybe that
could be of use to you as well?
[1] https://github.com/mauro3/Parameters.jl
[2] https://bitbucket.org/maurow/lmesh.jl
On Sat, 2015-06-20 at 19:16, Stef Kynaston <[email protected]> wrote:
> I feel I am missing a simpler approach to replicating the behaviour of a
> Matlab structure. I am doing FEM, and require structure like behaviour for
> my model initialisation and mesh generation. Currently I am using composite
> type definitions, such as:
>
> type Mesh
> coords :: Array{Float64,2}
> elements :: Array{Float64,2}
> end
>
> but in actuality I have many required fields (20 for Mesh, for example). It
> seems to me very impractical to initialise an instance of Mesh via
>
> mesh = Mesh(field1, field2, field3, ..., field20),
>
> as this would require a lookup of the type definition every time to ensure
> correct ordering. None of my fields have standard "default" values.
>
> Is there an easier way to do this that I have overlooked? In Matlab I can
> just define the fields as I compute their values, using "Mesh.coords =
> ...", and this would work here except that I need to initialise Mesh before
> the "." field referencing will work.
>
> First post, so apologies if I have failed to observe etiquette rules.