liveBook · Manning

Type hints

As explained in Julia as a Second Language

A type annotation is syntax that associates a variable or expression with an explicitly specified type.

A type annotation uses `::` to specify a type for a variable or expression. In `name::String`, the name is expected to hold a string. Annotations are especially useful in composite types, where each field can have its own type rather than sharing a broad value type such as `Any` in the dictionary example. section 7.1

Listing 7.1 Definition of a composite type
struct Archer
    name::String   #1
    health::Int    #2
    arrows::Int    #3
end

The `Archer` definition gives its name, health, and arrows fields explicit types. section 7.1

An annotation can also trigger conversion when a value is assigned or returned. Assigning `Int8(10)` to a `Float64` field attempts to convert it; a function annotated to return `UInt8` likewise attempts to convert its result. Conversion is not guaranteed to succeed. section 9.2

julia> foo(x::Int64) :: UInt8 = 2x
foo (generic function with 1 method)

julia> y = foo(42)
0x54

julia> typeof(y)
UInt8

The return annotation makes `foo(42)` produce a `UInt8` value. section 9.2

For reusable composite types, fields can use a type parameter: `struct Vec2D{T}; Δx::T; Δy::T; end`. This avoids writing separate structures for different numeric types. Leaving the field annotations out is possible, but gives up the type checking and memory and performance benefits described for typed fields. section 18.2

How Julia as a Second Language covers it

How deep it goes
Works through syntax, field and return examples, and conversion; defers a detailed account of the costs of unannotated fields.
Examples
Shows a typed character structure, a converted numeric field and function result, and typed versus unannotated vector structures.
What it assumes
Introduces `::` with composite-type fields, then uses familiarity with structs and numeric types for later examples.
What you can do after
Build it from scratch by annotating Julia fields and returns, and recognize when those annotations prompt conversion.
How it connects
Connects annotations to composite types, implicit conversion, parametric types, and JIT type checking.

Also explained in

Compare how 17 books explain type hints →

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.