liveBook · Manning

Data type

As explained in Julia as a Second Language

A type is a classification of values that determines their representation and the operations applicable to them in a programming language.

A data type determines what an object can do. In Julia, `typeof(42)` returns `Int64`, while `typeof('A')` returns `Char`. An array stores elements in order; a dictionary supports lookup by key. Type also matters to control flow: `true` can be used as an `if` condition, but `2.5` cannot. section 7.0

Types become useful when an application needs its own objects. A product catalog could represent each screw only by a number and use sets to find matching products. Alternatively, a `Screw` type can keep its product number, head type, drive style, and material together. Giving those attributes custom types helps catch an illegal category assigned to a field. section 13.5

struct Screw
   prodnum::Int
   headtype::HeadType
   drivestyle::DriveStyle
   material::Material
end

The `Screw` definition shows how typed fields turn separate product attributes into one object. section 13.5

Types also shape everyday operations:

  • Use a target type as a constructor for number conversion: `Float64(1//2)` produces `0.5`. Conversion can fail when the target cannot represent the value, as with `Int8(1000)` or `Int64(4.5)`. section 9.2
  • Julia can call `convert` implicitly when assigning to a typed array element or composite field, or when a typed function returns. Parsing a number from a string is a different operation. section 9.2
  • Types are objects themselves: they can be passed around, stored, and used by methods. section 9.2

Types have a performance role too. Julia fixes a composite type’s fields and their declared types: an instance cannot gain or lose fields, or store a value that does not match a field type. These restrictions help its JIT compiler generate optimized code. Parametric types matter here because they can help avoid the bookkeeping associated with boxed values. section 18.4 section 18.4

How Julia as a Second Language covers it

How deep it goes
Works through type inspection, typed fields, conversion, and performance intuition; defers a fuller explanation of the parametric `Type` form.
Examples
Uses `typeof` REPL checks, numeric conversions, a typed screw catalog, and a vector-scaling performance sketch.
What it assumes
Builds from arithmetic with integers, floating-point numbers, and fractions before introducing types as a foundation for richer applications.
What you can do after
Build it from scratch: define application-specific objects with typed fields, and anticipate some conversion and performance consequences.
How it connects
Connects types to multiple dispatch, composite types, conversion, parametric types, boxing, and JIT compilation.

Also explained in

Compare how 18 books explain data 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.