Skip to content

Sets, Data and maps

Three kinds of value use braces or hold keys and values, and they are easy to mix up. They are different types with different uses:

Type What it is Written
Set<T> distinct values of one type, with no positions {1, 2, 3}
Data a tree of named fields of any types, the shape of JSON {title: "Dune", pages: 412}
Map<K, V> keys of one type, each to a value of one type Map(["dune"], [412])

The colon tells braces apart

Braces holding values are a set. Braces holding key: value entries are Data. The colon is the only difference, and the empty forms keep it: {} is the empty set and {:} is the empty Data record.

d2 diagram

{3, 1, 3, 2}
{3, 1, 2}
{title: "Dune", pages: 412}
{pages: 412, title: "Dune"}
{}
{}
{:}
{:}

A set keeps each value once. A Data record prints its keys in order, so the same tree always prints the same way.

{} has a definite meaning, the empty set, so it is not an empty record:

d : Data = {}
d
check error[type-mismatch]: `d` is declared Data but its value is Set<Never>
 --> <expression>:1:12
  |
1 | d : Data = {}
  |            ^^

{} is a Set<Never>: a set with no elements, and so no element type yet. A declared set type gives it one:

seen : Set<Int> = {}
seen | size
0

Data is not a map

Data is the type of values whose shape is not known to the program: what a JSON or YAML document decodes to. A record's fields may hold different types, a number beside a text beside a list, and nesting goes as deep as the document does. It is not a hash map, and not a Map<String, Any>.

A Map<K, V> is typed: every key is a K and every value a V. The constructor pairs a list of keys with a list of values:

Map(["dune", "emma"], [412, 474])
Map {"dune": 412, "emma": 474}

Use a set for a group of distinct values, a map to look up one value per key, and Data for a document whose shape the program does not control. Maps covers maps in full.

{:} is Data, or a declared map

{:} has no type of its own. Where nothing says otherwise it is the empty Data; where a map type is declared for it, it is that empty map:

Written Type Value
{:} with nothing around it Data the empty Data record, printed {:}
d : Data = {:} Data the same
m : Map<String, Int> = {:} Map<String, Int> an empty map, equal to Map<String, Int>([], [])
m : SortedMap<String, Int> = {:}, or OrderedMap that map type an empty map of that kind
f({:}) where f takes a Map<String, Int> Map<String, Int> an empty map
counts : Map<String, Int> = {:}
counts == Map<String, Int>([], [])
true

{:} is Data unless a map type is declared for it. An empty literal has no entries to infer a type from, so the declared type decides.

This applies to the empty literal only. A non-empty literal with keys is always Data, and binding it to a map type is a type error:

m : Map<String, Int> = {a: 1}
m
check error[type-mismatch]: `m` is declared Map<String, Int> but its value is Data
 --> <expression>:1:24
  |
1 | m : Map<String, Int> = {a: 1}
  |                        ^^^^^^

A non-empty map is built with the constructor, Map(["a"], [1]).

What is next

Chapter 3 covers lists, sets and maps, and the steps they share.