Types and collections – inner constructors

Here is another type with only default constructors:

# see the code in Chapter 6\inner_constructors.jl
type Person
    firstname::String
    lastname::String
    sex::Char
    age::Float64
    children::Array{String, 1}
end
p1 = 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("Alan","Bates",'M',45.5,String["Jeff","Stephan"]),
 Person("Julia","Smith",'F',27.0,String["Viral"])] ...

Get Julia: High Performance Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.