How to create an array of preallocated mutable structs

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…

Use an array comprehension:

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

Alternatively:

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

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

5 Likes