Skip to content

feat: add Map collection type - #98

Merged
vito merged 5 commits into
mainfrom
map-collection-type
Jun 9, 2026
Merged

vito merged 5 commits into
mainfrom
map-collection-type

Conversation

@vito

@vito vito commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Adds a string-keyed Map collection type to Dang, alongside the existing List.

Syntax

pub roles = ["alice": "admin", "bob": "user"]   # map literal
pub empty: Map[Int!] = [:]                        # empty map
roles["alice"]                                    # indexing; missing key -> null
pub dyn = [k: 42]                                 # dynamic (non-literal) keys

Maps are typed as Map[V]. Keys must be strings and values must be homogeneous; both are enforced at type-check time with dedicated errors. Maps preserve insertion order and compare order-independently.

Stdlib

get, has, length, isEmpty, keys, values, with, without, merge, map, and each — all immutable (they return new maps rather than mutating in place). keys/values preserve insertion order.

Implementation

Covers the type system, parser grammar (dang.peg + regenerated peg parser), the tree-sitter grammar/parser, AST literals, evaluation, completion, formatting, and docs. Includes tests/test_maps.dang, error-case fixtures with golden files, a tree-sitter corpus, and runnable stdlib examples for each new method.

Tests

go test ./pkg/dang/ ./tests/ passes. The pre-existing pkg/lsp neovim test (TestNeovimRename) flakes on main independently of this change.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: d642769
Status: ✅  Deploy successful!
Preview URL: https://03934fd3.dang-3kk.pages.dev
Branch Preview URL: https://map-collection-type.dang-3kk.pages.dev

View logs

@sagikazarmark

Copy link
Copy Markdown
Contributor

indexing; missing key -> null

Adding non-null coercion becomes increasingly important: #53 (comment)

@vito
vito force-pushed the map-collection-type branch from decc64e to 49e50eb Compare June 6, 2026 18:36
@vito

vito commented Jun 6, 2026

Copy link
Copy Markdown
Owner Author

@sagikazarmark done in #100

vito added 4 commits June 8, 2026 20:53
Add a string-keyed Map collection type alongside List. Maps are written
with `["key": value]` literals (and `[:]` for an empty map), typed as
`Map[V]`, and indexed with `m["key"]`, returning null for missing keys.

Values must be homogeneous and keys must be strings; both are enforced at
type-check time with dedicated errors. Maps preserve insertion order and
compare order-independently. The stdlib provides get, has, length,
isEmpty, keys, values, with, without, merge, map, and each, all returning
new maps rather than mutating in place.

Includes parser grammar, regenerated peg and tree-sitter parsers, stdlib
methods, completion, formatting, docs, and tests.

Signed-off-by: Alex Suraci <[email protected]>
A new test on main requires every stdlib builtin to carry a runnable
.Example(...) so the reference can render a REPL for each entry. Add
examples for the Map methods, which also exercise each one through the
parse/infer/eval path.

Signed-off-by: Alex Suraci <[email protected]>
The initial `keys := m.Keys` was overwritten in both branches, which the
ineffassign linter flagged. Copy the keys once up front and only append
the new key when it isn't already present; behavior is unchanged.

Signed-off-by: Alex Suraci <[email protected]>
Main's formatter drops the redundant `pub` keyword from type-annotated
top-level declarations, which is now the canonical form. Reformat the map
test files to match and refresh the affected error-message golden.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the map-collection-type branch from 49e50eb to c9ce289 Compare June 8, 2026 21:02
These map test declarations are local bindings for assertions, not module
exports, so let reads better than the bare (formerly pub) form left by the
formatter. Convert them throughout and refresh the affected golden files.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit d1e446f into main Jun 9, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants