File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1
1
Either
2
2
======
3
3
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
+
4
35
API Reference
5
36
-------------
6
37
You can’t perform that action at this time.
0 commit comments