Types and collections – inner constructors

Here is another type with only default constructors:

# see the code in Chapter 6\inner_constructors.jlmutable struct Person    firstname::String    lastname::String    sex::Char    age::Float64    children::Array{String, 1}endp1 = Person("Alan", "Bates", 'M', 45.5, ["Jeff", "Stephan"])

This example demonstrates that an object can contain collections, such as arrays or dictionaries. Custom types can also be stored in a collection, just like built-in types, for example:

people = Person[]

This returns 0-element Array{Person,1}:

push!(people, p1)push!(people, Person("Julia", "Smith", 'F', 27, ["Viral"]))

The show(people) function now returns the following output:

Person[Person("Alan", "Bates", 'M', 45.5, ["Jeff", ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.