v6 Semantics: static types on a dynamic runtime

Lesson, slides, and applied problem sets.

View Slides

Lesson

v6 Semantics: type checking

This stage enforces static types before code generation.

Core checks

  • every expression has a type
  • assignments are type-compatible
  • function calls match param types
  • returns match declared return type

Inference rules

  • let x = 1; infers number
  • let xs = []; is an error (needs annotation)

Type errors (examples)

  • let x: number = "hi"; => type mismatch
  • true + 1 => invalid operands
  • fn f(a: number) -> number { return "x"; } => return type mismatch

Collections

  • arrays are homogenous
  • maps have consistent key/value types
  • map keys must be number/bool/string

Module Items