I have the following two structs. When I initialize the struct with a constructor, an array is created, but it is not what I expected.
using Distributions
mutable struct XYZ
x::Float64
y::Float64
z::Float64
end
mutable struct Coords
r::Vector{XYZ}
end
""" Make a structure called test that contains a vector of type XYZ
I want to have 10 XYZ structs in the "r" Vector """
Coords() = ( rand(Uniform(0,1.0),10,3) )
test = Coords()
I want to access test
by going test.r.x[i]
, however Julia complains that type Tuple has no field r. What it does create is a 2 dimensional array of size 10x3 and I can call elements via test[i,j]
but this is far from what I want. I want to have other arrays/variables in the composite with callable names...
I tried initializing this way as well
XYZ() = (rand(),rand(),rand())
Coords() = ([ XYZ() for i in 1:10 ])
test = Coords()
I still get the same warning. It seems like I have created a tuple rather than a composite type. How do I create a composite type that has arrays/vectors inside of other structs?
I am using Julia version 1.0.2
(expr)
does not create a 1-tuple -- that's equivalent to justexpr
. To actually create a 1-tuple (which you don't actually need here), write(expr,)
, like in Python. – phipsgabler