How to create an array of preallocated mutable structs


#1

Let’s say I want to preallocate 100 Point objects in an array… how?

julia> mutable struct Point 
         x::Float64
         y::Float64
       end

The fill function come to mind but it does not work because only one Point is made and all 100 entries in the array point to the same object…


#2

Use an array comprehension:

[Point(x, y) for x in 1:4, y in 1:6]

#3

Alternatively:

Point.(1:4, (1:6)')

(I really like the dot-broadcast notation in Julia.)