Skip to content

Commit fc07f45

Browse files
committed
Improve docs on set-theoretic syntax reference
1 parent 4c10b43 commit fc07f45

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/elixir/pages/cheatsheets/types-cheat.cheatmd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ empty_map()
8282

8383
# Has the keys name and age and may have other keys (open map)
8484
%{..., name: binary(), age: integer()}
85+
86+
# Has the key name, may have other keys, but age is not set
87+
%{..., name: binary(), age: not_set()}
8588
```
8689

8790
#### Maps with domain keys (domain keys are always treated as optional)
@@ -94,6 +97,18 @@ empty_map()
9497
%{..., atom() => binary(), binary() => binary()}
9598
```
9699

100+
#### Maps with mixed keys
101+
102+
```elixir
103+
# Has atom keys with binary values but a `:root` key of type integer
104+
%{atom() => binary(), root: integer()}
105+
106+
# Has atom keys with binary values but a `:root` key of type integer, and may have other keys
107+
%{..., atom() => binary(), root: integer()}
108+
```
109+
110+
#### Domain keys are `atom()`, `binary()`, `integer()`, `float()`, `fun()`, `list()`, `map()`, `pid()`, `port()`, `reference()`, `tuple()`
111+
97112
### Non-empty lists
98113

99114
#### Proper lists

lib/elixir/pages/references/gradual-set-theoretic-types.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,30 @@ Currently, the type system only tracks the top of each individial type as the do
118118
%{list(integer()) => integer(), list(binary()) => binary()}
119119
```
120120

121-
That's the same as:
121+
That's the same as specifying all lists:
122122

123123
```elixir
124-
%{list(term()) => integer() or binary()}
124+
%{list() => integer() or binary()}
125125
```
126126

127+
The supported domain keys are `atom()`, `binary()`, `integer()`, `float()`, `fun()`, `list()`, `map()`, `pid()`, `port()`, `reference()`, and `tuple()`.
128+
127129
Furthermore, it is important to note that domain keys are, by definition, optional. Whenever you have a `%{integer() => integer()}`and you try to fetch a key, we must assume the key may not exist (after all, it is not possible to store all integers as map keys as they are infinite).
128130

131+
#### Mixed keys
132+
133+
It is also possible to mix domain and atom keys. For example, the following map says that all atom keys are of type `binary()`, except the `:root` key, which has type `integer()`:
134+
135+
```elixir
136+
# Closed map
137+
%{atom() => binary(), root: integer()}
138+
139+
# Open map
140+
%{..., atom() => binary(), root: integer()}
141+
```
142+
143+
The order of the keys is of increasing precision. `:root` is more precise than `atom()`, therefore it comes later. This mirrors the runtime semantics of maps, where duplicate keys override the value of earlier ones.
144+
129145
### Functions
130146

131147
You can represent all functions as `function()`. However, in practice, most functions are represented as arrows. For example, a function that receives an integer and return boolean would be written as `(integer() -> boolean())`. A function that receives two integers and return a string (i.e. a binary) would be written as `(integer(), integer() -> binary())`.

0 commit comments

Comments
 (0)