Skip to content

Commit 8ba8ad7

Browse files
committed
Document halt responsibilities for resources
Closes #15265. Closes #15266.
1 parent 37dc125 commit 8ba8ad7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/elixir/lib/collectable.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ defprotocol Collectable do
6969
iex> Enum.into([1, 2, 3], MapSet.new())
7070
MapSet.new([1, 2, 3])
7171
72+
## Halting
73+
74+
The `:halt` flag will be given whenever the collection won't
75+
terminate correctly and must be used to clean up existing resources
76+
(such as sockets, file handles, etc).
77+
78+
Note it is not guaranteed that the accumulator given to halt will
79+
be the latest version of the accumulator returned by a previous call
80+
with `{:cont, elem}`. Therefore, you must track the collected results
81+
within the resource you intend to halt.
82+
83+
This is by design: ensuring halt is always called with the latest
84+
accumulator would make pure collectables (the ones that do not implement
85+
halt) expensive. However, given the collectables that must implement halt
86+
already need to track state, the burden of tracking the accumulator
87+
across invocations is put on them.
7288
"""
7389

7490
@type command :: {:cont, term} | :done | :halt

0 commit comments

Comments
 (0)