liveBook · Manning

Parameterized type

As explained in Julia as a Second Language

A parametric type is a type definition whose structure or behavior is specialized by one or more type parameters.

A parameterized type lets one definition serve several concrete types. In Julia, `Vector{Char}` is a vector whose elements are characters, while `UnitRange{Int64}` is a range with integer endpoints. Think of `P{T}` as a template: supply the type `T` to obtain a type you can use. section 10.2.1

Suppose a rocket simulation needs 2D positions and displacements. Integer and floating-point coordinates should not require separate definitions—and leaving field types unspecified gives up useful checks and performance. Put `{T}` after the struct name, then use `T` for both fields. section 18.0 section 18.2

Listing 18.4 Defining parametric types Point and Vec2D
"A point at coordinate (x, y)"
struct Point{T}
    x::T
    y::T
end
 
"A vector with displacement (Δx, Δy)"
struct Vec2D{T}
    Δx::T
    Δy::T
end
 
"Calculate magnitude of a vector (how long it is)"
norm(v::Vec2D) = sqrt(v.Δx^2 + v.Δy^2)

`Point{T}` and `Vec2D{T}` each use one parameter for two fields; `norm` works across the resulting vector types without a separate definition for each numeric type. section 18.2

Sharing `T` also makes a promise: both fields have the same type. `PVec2D(2, 3)` works, but `PVec2D(2, 3.0)` cannot infer one `T` and raises a `MethodError`. Supplying `PVec2D{Int}(2, 3.0)` instead specifies the field type and converts the second value. section 18.3

julia> struct GVec2D Δx; Δy end               #1
 
julia> struct PVec2D{T} Δx::T; Δy::T end      #2
 
julia> v = GVec2D(2, 3.0)                     #3
GVec2D(2, 3.0)
 
julia> u = PVec2D(2, 3.0)                     #4
ERROR: MethodError: no method matching PVec2D(::Int64, ::Float64)
 
julia> u = PVec2D(2, 3)                       #5
PVec2D{Int64}(2, 3)
 
julia> u = PVec2D{Int}(2, 3.0)                #6
 PVec2D{Int64}(2, 3)

Compare the unannotated and parameterized constructors, then try the explicit `{Int}` constructor to see how inference and conversion differ. section 18.3

You can narrow the permitted parameter with `struct Vec2D{T<:Number}`. In methods, `where T` can require related arguments to share a type—for example, a point and a vector—or separate parameters can allow different types. These choices make invalid inputs visible rather than silently accepting arbitrary field values. section 18.3 section 18.1

The payoff is not only fewer definitions. Concrete field types help Julia’s JIT compiler generate optimized code and reduce boxing; for an array such as `Vector{Point{Int32}}`, they also let it determine the space needed for its elements. Parameterization is therefore a practical tool for type safety, reuse, speed, and memory use. section 18.4 section 18.5

How Julia as a Second Language covers it

How deep it goes
Works through definitions, constructors, parameter bounds, method signatures, boxing intuition, and array layout without detailing garbage collection.
Examples
Uses Julia REPL constructor checks, 2D points and vectors, and an array of integer-coordinate points.
What it assumes
Builds on Julia structs and collections while introducing type parameters through familiar vectors, ranges, and pairs.
What you can do after
Build it from scratch: define numeric parameterized structs and methods that require matching or independently constrained type parameters.
How it connects
Connects parameterized types to collections, parametric methods, subtype constraints, boxing, and memory layout.

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.