Hey Eric,
Exactly right … your “blueprint” analogy is a good one and helpful. I often use the word “template”, which is essentially the same. This “blueprint” that is defined in the struct creates what we can consider to be a customised data-type. So, just as we can declare a variable with a uint
or string
data-type, for example, we can also declare a variable with a customised data-type using the struct name — in our case, with Person
. It’s like a data-type, because it is defining the type of value that the variable can hold: in our case a struct instance (like an object) containing 2 properties (age
holding a uint
value, and name
holding a string
value).
This helps us to understand …
… which is a array defined as containing instances (values) created from the Person
“blueprint”.
This isn’t correct …
people
is the name of the array, which contains instances based on our Person
“blueprint” (or Person
class, as you have called it here).
To create an instance based on a struct “blueprint”, we need to use the name of the struct. So, this is why we use Person
and not the name of the array people
in the following line …
We can create a struct instance in a single expression using parentheses. The values assigned to the properties must be placed between the parentheses in the same order as the properties are defined within the struct. This is why we don’t need to declare the property names. As age
is the first property within the Person
struct, 30
will be automatically assigned to the new instance’s age
property etc.
Within the the body of the addPerson function, in the first line (quoted above) a new Person
instance is created and assigned to a local variable called newPerson
. We could call this local variable anything, apart from the array name people
— we are not referencing the array in the first line of code. As you know, all variables in Solidity need to be declared with a specific data-type. Because this local variable is temporarily storing our new Person
instance, we need to declare it with our customised data-type, Person
. As a struct instance is a complex data-type, we also need to declare the data location of any local variable holding a struct instance.
In this second line we are pushing the newly created Person
instance (stored temporarily in the newPerson
variable) to the people
array. As in JavaScript, the push method is appended to the array it is called on using dot notation. The value to be pushed to (added to the end of) the array is the push method’s argument, and so is placed within the parentheses.
This is wrong, because…
-
.push()
is an array method, and so it can’t be called on a struct. The properties of a struct are fixed according to its definition (a fixed “blueprint”). We can’t add, remove or modify a struct’s properties. We can only modify the property values of individual instances created from the struct “blueprint”. - It’s trying to do the opposite of what we want to do.
Hopefully, this explanation makes things clearer and will also help you to understand the syntax in the getPerson function. But, just let me know if anything is still unclear, or if you have any more questions.