Skip to content

Add typed map[K, V] with Python-style literals and basic methods #35

@synapticvoid

Description

@synapticvoid

Summary

Introduce a built-in map[K, V] type with Python-like literals ({key: value}) and a minimal, type-safe method set.

Proposed Syntax

Type syntax

  • map[K, V]

Literal syntax

  • Empty map: {}
  • Non-empty map: {"a": 1, "b": 2}

Method syntax

  • m.len() -> Int
  • m.contains(key: K) -> Bool
  • m.get(key: K) -> KeyError!V
  • m.add(key: K, value: V) -> Unit (mutating; insert or replace)
  • m.remove(key: K) -> KeyError!V (mutating)

Type Inference Rules

  • const m = {"a": 1, "b": 2} infers map[str, int]
  • Empty map literals require annotation:
    • const m: map[str, int] = {}
    • var counts: map[str, int] = {}

Type Safety Rules

  • All keys must be assignable to K
  • All values must be assignable to V
  • Key type K must be hashable/equatable
  • No implicit any inference from map literals

Mutability Rule

  • const forbids rebinding
  • Mutating methods (add, remove) require a mutable receiver (var)
  • Example:
    • Invalid: const m = {"a": 1}; m.add("b", 2);
    • Valid: var m = {"a": 1}; m.add("b", 2);

Scope (Minimal)

  • Parser support for:
    • map[K, V] in type positions
    • map literals: {}, {"k": v}
    • method calls on map values (existing call/field syntax)
  • Type checker support for:
    • inference for non-empty map literals
    • key/value type validation
    • explicit-type requirement for empty map literals
    • map method signatures and mutable-receiver checks
  • Interpreter support for:
    • map runtime value
    • method dispatch/intrinsics for len, contains, get, add, remove

Out of Scope

  • User-defined generics
  • Full map method surface area
  • Ordered-map guarantees

Acceptance Criteria

  • Valid:
    • const m: map[str, int] = {"a": 1}
    • var m = {"a": 1}; m.add("b", 2);
    • const n: Int = {"a": 1, "b": 2}.len();
  • Invalid (type errors):
    • const m: map[str, int] = {1: 2}
    • const m: map[str, int] = {"a": "x"}
    • const m = {} (no annotation)
    • const m = {"a": 1}; m.add("b", 2);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions