Creating your own types
Up to this point we have only worked with the three fundamental data types Number, String, and Boolean. Values of these types convey basic ideas such as the age of a person or someone's address. However, it is often necessary to form data types that can express more complex ideas, such as an entire person with all of their attributes, for instance, their name, their age, and their body height. To do this, we can strap together multiple values of different data types in something called a type.
As you can see in the example, we have created our first own data type called Person. You can choose any name you want for your own data types. At this point in the course we can assume that you are able to deduce how the definition of a type works on a syntactic level just by looking at the example. A type comprises many so-called attributes which make up the type. In the example, a person has the attributes name, age, and height, all of which have their own data type symbolized by their corresponding type literal.
To create a value of our new data type, we can use the following syntax to initialize the type, resembling a function call. The values that are passed in the parentheses match the attributes of a Person in the order they are defined in the type.
Now, the variable steve contains a value of data type Person, with the attributes name set to "Steve", age set to 42, and height set to 1.78. To access the attributes of the variable, we can use the dot notation, as can be seen in the following example.
Output:
We can also nest user defined types, as can be seen in the following example. The attribute age of a Person has been replaced by the attribute date_of_birth of type Date. When accessing attributes of nested types, the dot notation can be used in series.
Output:
At this point, it should also become clear to you why the data types Number, String, and Boolean are called fundamental. If you traverse any user defined type (even nested ones), you realize that eventually they are all made up of a combination of these three fundamental types.