I have a vector of a struct and want a vector of a component of the struct:
struct MyPair
x::Float64
y::Float64
end
vec = Vector{MyPair}()
push!(vec, MyPair(1,10))
push!(vec, MyPair(2,20))
push!(vec, MyPair(3,30))
println(vec[:].x) # Doesn't compile.
println(vec.x[:]) # Doesn't compile, either.
Does a functionality to do this already exist in Julia or in a Julia package?
I can easily write a function to extract the vector of the component but it’s a bit tedious to have to write such a function for each struct I define . . .
(Before introducing the struct, I was using a 2D array like arr[1,:] for vec.x and arr[2,:] for vec.y but I always get confused as to whether “1” represents “x” or not . . . The struct introduces an annotation, if you like.)