ZIO provides a variety of features for building synchronous, asynchronous and concurrent applications and enables us to use functional effects in Scala.
- Why functional effects?
- How to use functional effects and data types built on ZIO?
- How to manage errors and recover from them?
- How to manage resources and how to make concurrent tasks?
4. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
5. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
def toInt(str: String): Try[Int] =
Try(str.toInt)
6. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
def toInt(str: String): Try[Int] =
Try(str.toInt)
toInt("One")
7. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
def toInt(str: String): Try[Int] =
Try(str.toInt)
toInt("One")
Failure(java.lang.NumberFormatException: For input string: "One")
8. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
def nextDay(day: DayOfWeek): String =
day.plus(1) .toString
assert(nextDay(THURSDAY)))(equalTo("FRIDAY"))
9. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
def nextDay(day: DayOfWeek): String =
day.plus(1) .toString
assert(nextDay(THURSDAY)))(equalTo("FRIDAY"))
10. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
def add(pasta: Pasta, sauce: Sauce,
water: Water): Cooked[Pasta] = {
val p = water.boil.map(_.put(pasta))
p.addSauce(sauce)
}
11. Stay home safe and
use what you have
No Side effects
Pure Function
Trust your types
Total
Test your functions
Deterministic
12. Effects are useful
❏ handle events
❏ Send messages
❏ read from the DataBase
❏ persist information
❏ print out the result
❏ retry in event of errors
❏ send result to other
services
❏ ...
Real World Applications
55. Error Management
Make defects as expected errors:
val io: IO[String, Nothing] =
IO.succeed(throw new Exception("💣"))
.unrefine(e => s"The error is: $e")
58. Error Management
def simpleName[A](c: Class[A]) = c.getSimpleName
object Example
Task(simpleName(Example.getClass))
[error] java.lang.InternalError: Malformed class name
[error] at java.lang.Class.getSimpleName(Class.java:1330)
87. Resource Management
● You can use the methods provided by the dependencies in your
program.
● once you provide a layer or an environment, the implementation
will be acquired and used then released at the end.
ZIO Environment & ZLayer[R, E, A]
90. CREDITS: This presentation template was
created by Slidesgo, including icons by Flaticon,
and infographics & images by Freepik
THANKS
Follow me: @WiemZin
Please keep this slide for attribution