Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/design/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@ arbitrary nesting (e.g. `list<map<string, list<int>>>`).
| `map<K,V>` | `BTreeMap<Value, Value>` | key/value pairs, sorted keys |
| `set<T>` | `BTreeSet<Value>` | unique elements, membership |

## Empty literals: `{}` is a set, `{:}` is a map

`map` and `set` share brace syntax, so an empty `{}` is ambiguous on its face.
Mux resolves this in the grammar rather than the type system: **`{}` is always
the empty set, and the empty map is spelled `{:}`.**

The alternative - inferring which one `{}` meant from the surrounding expected
type - is what the compiler used to do, via a third `Type::EmptySetOrMap` that
every stage had to carry and a span-keyed override map that rewrote the type
after the fact. It worked, but the ambiguity leaked into semantics and codegen,
and an empty literal with no expected type to resolve against had no answer.
`{:}` removes the ambiguity at the source, so `EmptySetOrMap` and the override
machinery are gone (mux-compiler#266).

Consequences worth knowing:

- `{}` in a map-typed position is a compile error, not an inference. Both
directions of the mix-up get a targeted diagnostic naming the other spelling.
- Empty literals still need an explicit type - `{:}` alone cannot infer `K`/`V`,
the same way `[]` cannot infer its element type.
- Nesting follows the same rule per position: `map<int, set<int>> x = {1: {}}`
is a map of sets, while `map<int, map<int, int>> y = {1: {:}}` is a map of maps.

## Why BTree, not Hash

`map` and `set` use the B-tree variants rather than hash maps for:
Expand Down
2 changes: 1 addition & 1 deletion llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repo owns the facts that span more than one repo.
- [Monomorphization](https://github.com/muxlang/mux-context/blob/main/docs/design/monomorphization.md): compile-time generics
- [Object system](https://github.com/muxlang/mux-context/blob/main/docs/design/object-system.md): ObjectRef, type registry, static interface dispatch
- [Operators](https://github.com/muxlang/mux-context/blob/main/docs/design/operators.md): short-circuit lowering, operator overloading
- [Collections](https://github.com/muxlang/mux-context/blob/main/docs/design/collections.md): list/map/set, BTree rationale, nesting
- [Collections](https://github.com/muxlang/mux-context/blob/main/docs/design/collections.md): list/map/set, BTree rationale, nesting, empty literals (`{}` is a set, `{:}` is a map)
- [Error handling](https://github.com/muxlang/mux-context/blob/main/docs/design/error-handling.md): result/optional layout
- [Runtime panics](https://github.com/muxlang/mux-context/blob/main/docs/design/panics.md): process-terminating failures, the panic format, why no baked source snippet
- [Modules](https://github.com/muxlang/mux-context/blob/main/docs/design/modules.md): import resolution, name mangling, module init
Expand Down
Loading