What's the difference between Task and IO in Scalaz?
Asked Answered
H

1

10

These two Scalaz types

seem very conceptually similar. They both:

  • Represent a potentially side-effecting computation
  • Produce a success (A) or failure (Exception) result
  • Have Monad instances
  • Can be unsafely unwrapped with run or unsafePerformIO

How do they differ? Why do they both exist?

Huneycutt answered 28/8, 2015 at 8:26 Comment(3)
Check groups.google.com/forum/#!topic/scalaz/mhh_NlkHTFk, includes explanation from the creator of TaskBonita
@Bonita Thanks - which one of them is the creator of Task?Huneycutt
It was Chiusano, AFAIKBonita
G
6

Core difference is that IO simply delays the execution of something but does it within a current thread. Task on the other hand is capable of executing something concurrently (thus the implicit ExecutorService).

Additionally, Task carries the semantics of scalaz's Future (Future that is more compossible than the classic scala version; Future that allows you to have higher control of concurrency by making forking explicitly defined and not execute tasks in parallel as soon as instantiated ). Furthermore, if you read the source for scalaz's Future it will point you to Task as a more robust version that can be used in prod.

Finally, note that attemptRun of the Task returns \/[Throwable, A] while unsafePerformIO of IO just returns A. This speaks to more robust treatment of real life error scenarios.

As far as I know, everywhere you would use IO to compose effects you would use Task in real production codebase.

Here is a good blog post about Task usage: Tim Perrett's Task Post

Grandmother answered 10/9, 2015 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.