I've tried to create an array of structs, but I've been frustrated (structs seem to lose their fields when put into arrays), so I turned to using StructArrays. This seems to work, but I'm not able to change their fields.
Example code:
# testing arrays of structs
using StructArrays
mutable struct Point
x :: Float64
y :: Float64
end
function __init__()
s = [ Point( rand(), rand() ) for i in 1:10] # array of 10 structs, with random x,y fields
sArray = StructArray(s) # turn this into a StructArray
println("\n structArray",sArray,"\n")
for i = 1:10
println("\nbefore: ", i," ", sArray[i])
println(" ] ", sArray[i].x)#, z.x)
print("x was: ", sArray[i].x , "; should be: ", sArray[i].x /2)
sArray[i].x /= 2
sArray[i].y = rand()
println("\nafter: ", i," ", sArray[i],"\n\n")
end
end
As you can see from the results below, none of the fields are changing (compare 'before' with 'after'):
before: 1 Point(0.4055949800691312, 0.11581742286491026)
] 0.4055949800691312
x was: 0.4055949800691312; should be: 0.2027974900345656
after: 1 Point(0.4055949800691312, 0.11581742286491026)
before: 2 Point(0.8447305147234605, 0.34071870401541093)
] 0.8447305147234605
x was: 0.8447305147234605; should be: 0.42236525736173025
after: 2 Point(0.8447305147234605, 0.34071870401541093)
...
Any ideas? I'm not tied to struct arrays, I just thought I would be able to use syntax something like this:
myPoint[1].x = 640
myPoint[1].y = 480