1
- # An error typed Future
1
+ # An error typed Future[ +E, +A ]
2
2
[ ![ Scala Steward badge] ( https://img.shields.io/badge/Scala_Steward-helping-blue.svg?style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAQCAMAAAARSr4IAAAAVFBMVEUAAACHjojlOy5NWlrKzcYRKjGFjIbp293YycuLa3pYY2LSqql4f3pCUFTgSjNodYRmcXUsPD/NTTbjRS+2jomhgnzNc223cGvZS0HaSD0XLjbaSjElhIr+AAAAAXRSTlMAQObYZgAAAHlJREFUCNdNyosOwyAIhWHAQS1Vt7a77/3fcxxdmv0xwmckutAR1nkm4ggbyEcg/wWmlGLDAA3oL50xi6fk5ffZ3E2E3QfZDCcCN2YtbEWZt+Drc6u6rlqv7Uk0LdKqqr5rk2UCRXOk0vmQKGfc94nOJyQjouF9H/wCc9gECEYfONoAAAAASUVORK5CYII= )] ( https://scala-steward.org )
3
3
4
- A thin wrapper on the Future monad for the purpose of giving it an error type.
5
- Designed to be an alternative for the ` scala.concurrent.Future ` with minimal migration needed. Entirely built on top
4
+ A thin wrapper on the Future monad for the purpose of giving it a type parameter for the error channel, giving you
5
+ the power to see _ how_ a Future can fail just as clearly as how it can succeed.
6
+ Designed to be an alternative for the ` scala.concurrent.Future ` with minimal migration needed, entirely built on top
6
7
of the Future, it has
7
8
the same performance and easily integrates into existing Future
8
9
based libraries.
@@ -20,19 +21,19 @@ still want typed errors feel free to use this library, or copy the code to your
20
21
Setup via ` build.sbt ` :
21
22
22
23
``` sbt
23
- libraryDependencies += " io.github.ragazoor" %% " task" % " 0.1.15 "
24
+ libraryDependencies += " io.github.ragazoor" %% " task" % " 0.1.16 "
24
25
```
25
26
26
27
# Getting Started
27
28
28
- In this library the main monad is called a ` Task ` , which has the type signature ` Task[+E, +A] ` .
29
+ In this library the monad is called a ` Task ` , which has the type signature ` Task[+E, +A] ` .
29
30
This ` Task ` is just a thin wrapper on top of the Future we know from Scala, which we have defined here as the
30
- type alias ` type Future[+A] = Task[Throwable, A] ` . This is so that there is less migration needed if you were to adopt this library.
31
+ type alias ` type Future[+A] = Task[Throwable, A] ` . This is to keep backward compatability if you were to adopt this library.
31
32
32
33
## Examples
33
34
34
35
In ` io.github.ragazoor.implicits._ ` there is an implicit class that
35
- allows you to convert from an ` StdFuture ` to a ` Task ` using ` .toTask ` .
36
+ allows you to convert from an ` scala.concurrent.Future ` to a ` Task ` using ` .toTask ` .
36
37
``` scala
37
38
import common .User
38
39
import io .github .ragazoor .Future
@@ -47,7 +48,7 @@ class UserExample(userRepo: UserRepository) {
47
48
def getUser (id : Int ): Future [User ] = // Future[User] is an alias for Task[Throwable, User]
48
49
userRepo
49
50
.getUser(id) // This returns a scala.concurrent.Future
50
- .toTask // Converts to Task
51
+ .toTask // Converts to Task[Throwable, User]
51
52
}
52
53
```
53
54
@@ -73,20 +74,19 @@ trait UserProcess {
73
74
class UserServiceFutureExample (userProcess : UserProcess )(implicit ec : ExecutionContext ) {
74
75
75
76
def processUser (userTask : Task [User ]): Task [Throwable , User ] =
76
- userProcess.process(userTask) // Here Task -> Future conversion is implicit
77
+ userProcess.process(userTask) // Using scala.concurrent.Future as input and output, implicit conversion
77
78
.toTask
78
79
79
80
// Does the same thing without implicits, but more migration needed
80
81
def processUser2 (userTask : Task [User ]): Task [Throwable , User ] =
81
- userProcess.process(userTask.toFuture) // Here Task -> Future conversion is explicit
82
+ userProcess.process(userTask.toFuture) // Using scala.concurrent.Future as input and output, explicit conversion
82
83
.toTask
83
84
}
84
85
85
86
```
86
87
87
88
This is the basics for using the `Task` type in
88
- your code. The Task has the same API
89
- as the Future , and thanks to the type alias
89
+ your code. The Task has the same API as the Future , and thanks to the type alias
90
90
`type Future[+A] = Task[Throwable, A]` we don' t need to rename a lot of unnecessary renaming.
91
91
92
92
### Error handling
0 commit comments