Skip to content

Commit 684130d

Browse files
authored
Note about typespecs for functions which raise (#12568)
* Note about typespecs for functions which raise 'no_return' was removed from functions like `File.copy!/3` in #8092 In the comment thread, someone asked whether the documentation should mention that certain other functions may raise, but José responded: > Any function can fail any time in face of invalid input * Incorporate PR feedback
1 parent b6800b8 commit 684130d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lib/elixir/pages/typespecs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,14 @@ When using `iex`, the `IEx.Helpers.b/1` helper is also available.
311311
Elixir discourages the use of the `string()` type. The `string()` type refers to Erlang strings, which are known as "charlists" in Elixir. They do not refer to Elixir strings, which are UTF-8 encoded binaries. To avoid confusion, if you attempt to use the type `string()`, Elixir will emit a warning. You should use `charlist()`, `nonempty_charlist()`, `binary()` or `String.t()` accordingly, or any of the several literal representations for these types.
312312

313313
Note that `String.t()` and `binary()` are equivalent to analysis tools. Although, for those reading the documentation, `String.t()` implies it is a UTF-8 encoded binary.
314+
315+
## Functions which raise an error
316+
317+
Typespecs do not need to indicate that a function can raise an error; any function can fail any time if given invalid input.
318+
In the past, the Elixir standard library sometimes used `no_return()` to indicate this, but these usages have been removed.
319+
320+
The `no_return()` type also should not be used for functions which do return but whose purpose is a "side effect", such as `IO.puts/1`.
321+
In these cases, the expected return type is `:ok`.
322+
323+
Instead, `no_return()` should be used as the return type for functions which can never return a value.
324+
This includes functions which loop forever calling `receive`, or which exist specifically to raise an error, or which shut down the VM.

0 commit comments

Comments
 (0)