Skip to content

Commit 738c942

Browse files
committed
Documents Result monad
1 parent 958b9d1 commit 738c942

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/pages/either.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
Either
22
======
33

4+
Also known as ``Result``.
5+
6+
What is ``Result``? It is obviously a result of series of computations.
7+
It might return an error with some extra details.
8+
9+
``Result`` consist of two types: ``Success`` and ``Failure``.
10+
``Success`` represents successful operation result
11+
and ``Failure`` indicates that something has failed.
12+
13+
.. code:: python
14+
15+
from dry_monads.either import Result, Success, Failure
16+
17+
def find_user(user_id: int) -> Either['User', str]:
18+
user = User.objects.filter(id=user_id)
19+
if user.exists():
20+
return Success(user[0])
21+
return Failure('User was not found')
22+
23+
user_search_result = find_user(1)
24+
# => Success(User{id: 1, ...})
25+
26+
user_search_result = find_user(0) # id 0 does not exist!
27+
# => Failure('User was not found')
28+
29+
When it is useful?
30+
When you do not want to use exceptions to break your execution scope.
31+
Or when you do not want to use ``None`` to represent empty values,
32+
since it will raise ``TypeError`` somewhere
33+
and other ``None`` exception-friends.
34+
435
API Reference
536
-------------
637

0 commit comments

Comments
 (0)