Why not just use keyword arguments on an inner constructor?
julia> type MyType
a::Float64
b::Int64
MyType(;a = 0.0 , b=0) = new(a,b)
end
julia> x = MyType(b=5, a=2.3)
MyType(2.3,5)
On Saturday, June 20, 2015 at 5:15:13 PM UTC-4, David P. Sanders wrote:
>
>
> Christoph's solution is neat.
>
> Another possibility is to just start with an empty object, by defining an
> inner constructor that
> does not define any of the fields, and then fill it up, as you were (IIUC)
> looking for.
> As far as I am aware, there is not any problem with doing this.
>
> type MyType
> a::Float64
> b::Int64
> c::UTF8String
> d::Vector{Int}
>
> MyType() = new()
> end
>
> t = MyType()
>
> t.a = 17.
> t.b = -3
> t.c = "Hello"
> t.d = [3, 4]
>
> Note that an error will occur if you try to read any field that has not
> yet been defined.
>
> David.
>
>
> El sábado, 20 de junio de 2015, 14:43:03 (UTC-5), Stef Kynaston escribió:
>>
>> 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.
>>
>