Until now, we have been dealing with the data types that were available to a user and provided by Julia. Now we will be exploring ways to use types that are not made available to us by Julia, and we need to create them in order to address the problems we are dealing or intend to deal with.
The most important keyword that we need to create a user-defined data type is called an
type. The following is an example of how to create a simple data type in Julia:
julia> type Person
name::String
age::Int64
end
julia> rahul = Person("rahul",27)
Person("rahul",27)
julia> typeof(rahul)
Person
julia> rahul.name
"rahul"
julia> rahul.age
27
This example is one of the simplest examples of what we can consider a user-defined type in Julia. Observing closely, the Person
type has two fields, namely name
and age
. The fields can easily be accessed by using a period (.
) notation.
Interestingly, if you try to...