liveBook · Manning

Type checking

As explained in Julia as a Second Language

Type checking is the process of verifying that operations are applied to compatible types.

Type checking verifies that values have suitable types for an operation. In statically typed languages, checks happen before a program runs. In dynamically typed languages such as Julia, values have types and checks happen at runtime. Julia combines dynamic typing with just-in-time compilation. section 1.1.1

A parametric method can require two arguments to share a type. In `linearsearch(haystack::AbstractVector{T}, needle::T) where T`, the search value must have the same type as the vector’s elements. Using `AbstractVector` and `Any` instead would not enforce that relationship. section 18.1

A mismatch caught at a function call

`linearsearch([4, 5, 8], 5)` returns `2`. Replacing `5` with `"five"` produces a `MethodError`: no matching method accepts a vector of `Int64` elements and a `String` search value. section 18.1

The same idea applies to stored data. `struct Vec2D{T}` with fields `Δx::T` and `Δy::T` gives both displacements a shared type parameter. Leaving the field annotations out removes valuable runtime type checking and can increase memory use and reduce performance. section 18.2

Listing 18.1 linearsearch in collection haystack for element needle
function linearsearch(haystack::AbstractVector{T}, needle::T) where T
   for (i, x) in enumerate(haystack)
       if needle == x
           return  i
       end
   end
   nothing
end

The complete `linearsearch` definition shows how one `where T` clause connects the vector element type to the search value. section 18.1

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)

The `Point` and `Vec2D` definitions show shared field types in structs, alongside a function that uses a `Vec2D`. section 18.2

How Julia as a Second Language covers it

How deep it goes
Demonstrates checks through a parametric method and typed fields, but defers detailed treatment of performance costs.
Examples
Uses a linear search that rejects a string search value for an integer vector, plus typed and untyped two-dimensional vector structs.
What it assumes
Assumes familiarity with Julia functions and structs while explaining type parameters through concrete signatures and field declarations.
What you can do after
Apply it via Julia parametric methods and structs to require matching argument or field types.
How it connects
Connects dynamic typing and just-in-time compilation to parametric methods, parametric types, and method dispatch.

Also explained in

Compare how 5 books explain type checking →

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.