Skip to content

Commit

Permalink
Add else example for case operator into RU (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
brain-geek committed Jan 5, 2017
1 parent d89aaf0 commit 03bb6d5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ru/lessons/basics/control-structures.md
Expand Up @@ -173,3 +173,21 @@ with
{:ok, jwt, full_claims} <- Guardian.encode_and_sign(user, :token),
do: important_stuff(jwt, full_claims)
```

Начиная с версии Elixir 1.3, конструкция `with` также начала поддерживать `else`:

```elixir
import Integer

m = %{a: 1, c: 3}

a = with {:ok, res} <- Map.fetch(m, :a),
true <- Integer.is_even(res) do
IO.puts "Divided by 2 it is #{div(res, 2)}"
else
:error -> IO.puts "We don't have this item in map"
_ -> IO.puts "It's not odd"
end
```

Это помогает структурировать код обработки ошибок с помощью сопоставления с образцом в общем блоке-обработчике. Значение, которое туда передаётся &mdash; первое же выражение, которое не сопоставилось в основном теле `with`.

0 comments on commit 03bb6d5

Please sign in to comment.