I am trying to learn more about the way to organize my Julia code. I know from the documentation that I can create a structure to hold some data like this:
## The structure below has lots of fields, I am just showing one as example
struct Person
nArms::Int64
...
end
p = Person(2)
println("Number of arms = ", p.nArms)
I would like to learn how to add a new field to the existing structure created above:
nLegs = 2
Such that I can now have a new variable:
pritln("Number of legs = ", p.nLegs)
If I create data during my code execution, is there any way that I can add it to an existing structure that does not have a field created for it? Or should I carry the new data using variables throughout the code (without a structure definition)?
If you want to have some mandatory as well as some optional fields, then you need to create an explicit constructor, or a struct with explicitly accessor methods (see Base.getproperty, Base.setproperty!) for details. E.g.,