liveBook · Manning

Constructor method

As explained in Julia as a Second Language

A constructor is a function or language mechanism that creates an instance of a type from supplied component values.

A constructor creates an instance of a type from supplied values. In Julia, a composite type has a constructor function with the same name. Julia supplies methods that accept one argument per field: one uses the field types as argument annotations, and another accepts arguments of any type. section 8.3

Additional constructor methods can make objects easier to create. For a fuel tank, a two-argument method can calculate the propellant for a full tank from its dry and total masses, then call the three-argument constructor. The methods function lists the available constructor methods. section 8.3

Listing 8.8 Creating full tanks when the amount of propellant is not specified
function FlexiTank(drymass::Number, totalmass::Number)
   FlexiTank(drymass, totalmass, totalmass - drymass)
end

MediumTank() = refill!(MediumTank(0))
LargeTank() = refill!(LargeTank(0))

The two-argument FlexiTank method derives the propellant mass; no-argument methods provide full MediumTank and LargeTank instances. section 8.3

An outer constructor is defined outside the composite type and adds a method to the existing constructor. An inner constructor is defined inside the struct and replaces Julia’s automatically supplied constructor methods. It uses new to create the instance; new is available only inside an inner constructor. section 8.4

Listing 8.9 Defining an inner constructor for FlexiTank
mutable struct FlexiTank <: Tank
    drymass::Float64
    totalmass::Float64
    propellant::Float64
 
    function FlexiTank(drymass::Number, totalmass::Number)
        new(drymass, totalmass, totalmass - drymass)         #1
    end
end

This inner constructor computes propellant from the two supplied masses. With this definition, callers cannot use the automatically supplied three-argument constructor to set propellant independently. section 8.4

Custom constructors can help initialize objects with valid values, though the checks or calculations must be explicitly implemented. They can also hide storage details: angle constructors accept degrees, minutes, and seconds while creating DMS values stored in seconds. section 8.9 section 9.3.1

Listing 9.3 Angle constructors for degrees, minutes, and seconds
Degree(degrees::Integer) = Minute(degrees * 60)
Degree(deg::Integer, min::Integer) = Degree(deg) + Minute(min)

function Degree(deg::Integer, min::Integer, secs::Integer)
   Degree(deg, min) + Second(secs)
end

function Minute(minutes::Integer)
   DMS(minutes * 60)
end

function Second(seconds::Integer)
   DMS(seconds)
end

Degree, Minute, and Second provide convenient ways to construct angles without requiring callers to calculate the stored seconds themselves. section 9.3.1

A type constructor can also convert a number: T(x) requests a value of type T. Not every conversion is possible; for example, Int8(1000) and Int64(4.5) raise errors. Julia uses convert rather than the constructor for implicit conversions. section 9.2

How Julia as a Second Language covers it

How deep it goes
Explains default, outer, and inner constructor methods with implementations, method inspection, and failed calls.
Examples
Builds full rocket tanks and angle values, and demonstrates successful and failed numeric conversions.
What you can do after
Builds custom Julia constructors to derive initial fields and provide convenient inputs.
How it connects
Connects constructor methods to composite types, multiple dispatch, type conversion, and promotion.

Also explained in

Compare how 18 books explain constructor method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.