Skip to main content
Version: 2.0.x

Task

Task[A] is a type alias for ZIO[Any, Throwable, A], which represents an effect that has no requirements, and may fail with a Throwable value, or succeed with an A.

note

In Scala, a type alias is a way to give a name to another type, to avoid having to repeat the original type again and again. It doesn't affect results of the type-checking process. It just helps us to have an expressive API design.

Let's see how the Task type alias is defined:

type Task[+A] = ZIO[Any, Throwable, A]

So a Task is equivalent to a ZIO that doesn't need any requirement, and may fail with a Throwable, or succeed with an A value.

Sometimes we know that our effect may fail, but we don't care about the type of that exception. This is where we can use Task. The signature of this type alias is similar to Future[T] and Cats IO.

If we want to be less precise and eliminate the need to think about requirements and error types, we can use Task. This type alias is a good starting point for anyone who wants to refactor an existing code base which is written with Cats IO or Monix Task.

Principle of Least Power

The ZIO data type is the most powerful effect in the ZIO library. It helps us to model various types of workflows. On the other hand, the type aliases are a way of specializing the ZIO type for less powerful workflows.

Often, we don't need such a piece of powerful machinery. So as a rule of thumb, whenever we require a less powerful effect, it's better to use the appropriate specialized type alias.

So there is no need to convert type aliases to the ZIO data type, and whenever the ZIO data type is required, we can use the most precise type alias to fit our workflow requirement.