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




