Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain why functions returns extra information with tuples #12824

Merged
merged 2 commits into from
Jul 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion lib/elixir/pages/getting-started/lists-and-tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
{:error, :enoent}
```

If the path given to `File.read/1` exists, it returns a tuple with the atom `:ok` as the first element and the file contents as the second. Otherwise, it returns a tuple with `:error` and the error description.
If the path given to `File.read/1` exists, it returns a tuple with the atom `:ok` as the first element and the file contents as the second. Otherwise, it returns a tuple with `:error` and the error description. `File.read/1` returns a tuple, not a list, because it always contains two values. As we will soon learn, Elixir allows us to _pattern match_ on such shapes. Returning a list would not bring any advantages.

Check failure on line 153 in lib/elixir/pages/getting-started/lists-and-tuples.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Emphasis style should be consistent

lib/elixir/pages/getting-started/lists-and-tuples.md:153:337 MD049/emphasis-style Emphasis style should be consistent [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.29.0/doc/md049.md

Check failure on line 153 in lib/elixir/pages/getting-started/lists-and-tuples.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Emphasis style should be consistent

lib/elixir/pages/getting-started/lists-and-tuples.md:153:351 MD049/emphasis-style Emphasis style should be consistent [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.29.0/doc/md049.md

Most of the time, Elixir is going to guide you to do the right thing. For example, there is an `elem/2` function to access a tuple item but there is no built-in equivalent for lists:

Expand Down
Loading