liveBook · Manning

Concrete type

As explained in Julia as a Second Language

A concrete type is a type that can be directly instantiated or used as the representation of values rather than serving only as an abstract classification.

A concrete type describes values you can create directly. In Julia, an abstract type instead groups related types in a hierarchy: you cannot instantiate it. Concrete types sit at the leaves of that hierarchy, because Julia does not allow a concrete type to subtype another concrete type. section 7.3.1 section 7.5

For a battle simulator, an abstract Warrior can group concrete Archer, Pikeman, and Knight types. Each concrete type defines its own fields: an Archer has a name, health, and arrows, while a Knight has a mounted state. Shared fields such as name and health are a convention here; Julia does not enforce them through the abstract type. section 7.3.1

Listing 7.5 Definition of types in battle simulator
abstract type Warrior end           #1
 
mutable struct Archer <: Warrior    #2
    name::String
    health::Int
    arrows::Int
end
 
mutable struct Pikeman <: Warrior
    name::String
    health::Int
end
 
mutable struct Knight <: Warrior
    name::String
    health::Int
    mounted::Bool                   #3
 end

Define Warrior as an abstract type and its three concrete subtypes as mutable structs. The mutable keyword allows their fields to change at runtime. section 7.3.1 section 7.5

julia> robin = Archer("Robin Hood", 34, 24)
Archer("Robin Hood", 34, 24)
julia> Warrior()
ERROR: MethodError: no constructors have been defined for Warrior

Constructing an Archer creates a value; attempting to construct a Warrior fails because Warrior is abstract. section 7.3.1

A composite type is a concrete type made from zero or more fields; a primitive type is a concrete type with a single value. Use typeof on an object to discover its type, and supertype or subtypes to explore the hierarchy. section 7.3.1 section 7.5

Abstract types and multiple dispatch make the concrete types useful together. A battle! method accepting two Warriors can receive an Archer and a Knight; the actual argument types determine which attack! method runs. section 8.9 section 7.4.2

How Julia as a Second Language covers it

How deep it goes
Explains instantiation, fields, subtype restrictions, and dispatch through a worked hierarchy, without detailing type-system internals.
Examples
Uses Archer, Pikeman, and Knight structs, an attempted Warrior constructor, and a battle between an Archer and a Knight.
What you can do after
Apply it in Julia to define and instantiate concrete subtypes for a simulator.
How it connects
Connects abstract types, composite types, primitive types, subtyping, and multiple dispatch.

Also explained in

Compare how 4 books explain concrete type →

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.