Zio: Make Fiber a sum type

Created on 24 Jan 2020  路  4Comments  路  Source: zio/zio

Currently, many methods on Fiber return an Option, which is a growing design smell, caused by the fact that there are, conceptually, two distinct types of fibers:

  • A real fiber. These fibers are executing effects until completion.
  • A non-real fiber. These fibers are created from pure values, or from combining existing fibers.

Having an imprecise single trait that is returning option for various methods occurs when a product type is used to emulate a sum type, which is the situation that exists now.

In order to be precise, we should make Fiber a sealed trait, and factor out two subtypes of Fiber, which exist inside the companion object of Fiber. These two subtypes will accurately reflect the capabilities of the underlying fiber type.

I would suggest one of the following two namings:

  • Fiber.Logical / Fiber.Physical. A logical fiber is one that is created from a value or a group of other fibers. A physical fiber is one that is executing an effect.
  • Fiber.Synthetic / Fiber.Natural. A synthetic fiber is one that is synthesized from a value or composition. A natural fiber is one that is executing an effect.

Then all methods that currently return Option because of the fiber type can be moved to Fiber.Physical / Fiber.Natural, and made to return values (not wrapped in Option).

Existing code can be updated to interact with the type of fiber it requires, or if it does not require a specific type, it can utilize the parent, Fiber, which exposes all methods common to both types of fibers.

good first issue

Most helpful comment

I will work on this.

All 4 comments

I was taking a look at Fiber and what would the necessary changes to make a Fiber a sum type and I could not help but wonder at this statement:

Then all methods that currently return Option because of the fiber type can be moved to Fiber.Physical / Fiber.Natural

Do you think this also applies to def poll: UIO[Option[Exit[Throwable, A]]] ?

I can see this still making sense for fibers created from pure values or from combining others but a fiber that can never fail or succeed would have a poll signature of type UIO[Exit[Nothing, Nothing]]] which seems to be the only place where this gets a bit awkward... UIO.succeed(Exit.halt(Cause.empty)) ?

I will work on this.

@saraiva132 Not poll, that's an exception, predates the other ones...

*almost all

Was this page helpful?
0 / 5 - 0 ratings