Type System
Core Idea
A Type describes the kind of value and the operations and structure associated with it.
Ask first: What Type is this?
Major Type Forms
- struct
- class
- enum
- actor
- protocol
- function types
- tuple types
- optional types
- collection types
- generic types
Major Distinctions
Value Type
Examples:
- struct
- enum
- tuple
Reference Type
Examples:
- class
- actor
Do not reduce this to "stack vs heap".
The important concept is value semantics vs reference semantics.
Type Safety
The compiler uses type information to detect invalid operations.
Example:
let age: Int = "18"
This is rejected because String is not Int.
Type Inference
Swift can often determine a value's type without an explicit annotation.
let count = 10
count is inferred as Int.
Type vs Instance
Type
↓ creates / describes
Instance
Example:
struct Person { ... }
let person = Person(...)
Person → Type
person → Instance