Filling an array of structs from file contents

I tried to reproduce add_element function, and it worked for me

mutable struct Foo
    name::String
    id::Int
end

add_element!(stored::Array{Foo}, x::Foo) = push!(stored, x)
add_element2!(stored::Vector{Foo}, x::Foo) = push!(stored, x)

A = [Foo("a", 1)]
add_element!(A, Foo("b", 2))
add_element2!(A, Foo("c", 3))
@show A  # A = Foo[Foo("a", 1), Foo("b", 2), Foo("c", 3)]

A = Foo[]
add_element!(A, Foo("a", 1))
@show A  # A = Foo[Foo("a", 1)]

About Julia-ways. If you just want to append a structure just use push! as it is common. Also, does myStruct is defined once you read a new line?

1 Like