-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
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() -> Intm.contains(key: K) -> Boolm.get(key: K) -> KeyError!Vm.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}infersmap[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
Kmust be hashable/equatable - No implicit
anyinference from map literals
Mutability Rule
constforbids 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);
- Invalid:
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);
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels