Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

From Discord: Missing Alternative Instance for Either #500

Closed
effect-bot opened this issue Sep 22, 2023 · 0 comments · Fixed by #499
Closed

From Discord: Missing Alternative Instance for Either #500

effect-bot opened this issue Sep 22, 2023 · 0 comments · Fixed by #499
Assignees

Comments

@effect-bot
Copy link

Summary

The chat is about a missing alternative instance for the Either type in the Effect-TS ecosystem. The user is pointing out that there is no Alternative instance defined for Either in the library.

Some key takeaways from the chat are:

  • The user is looking for an Alternative instance for Either in the Effect-TS ecosystem.
  • The Alternative type class represents types that have a choice between two alternatives.
  • The absence of an Alternative instance for Either means that certain operations and combinators defined for Alternative cannot be used directly with Either in the Effect-TS ecosystem.

Example article

Missing Alternative Instance for Either

In the Effect-TS ecosystem, the Either type is a common choice for representing computations that may result in either a successful value or an error. However, it seems that there is a missing Alternative instance for the Either type.

The Alternative typeclass provides a way to combine computations that have a choice between multiple alternatives. It includes the empty operation, which represents a computation that has no successful value, and the orElse operation, which allows choosing between two computations.

Let's take a look at an example to understand how the Alternative typeclass works. Suppose we have two computations that may result in an Either value:

import { Either, left, right } from '@effect/data/either'

const computation1: Either<string, number> = left('Error 1')
const computation2: Either<string, number> = right(42)

If we want to combine these computations using the Alternative typeclass, we would expect to be able to write code like this:

import { Alternative } from '@effect/data/alternative'

const combinedComputation: Either<string, number> = Alternative.orElse(computation1, computation2)

However, currently, the Alternative typeclass does not provide an instance for the Either type. This means that we cannot use the orElse operation directly on Either values.

To work around this limitation, we can define our own Alternative instance for the Either type. Here's an example implementation:

import { Alternative, Alternative1 } from '@effect/data/alternative'
import { Either, left, right } from '@effect/data/either'

const eitherAlternative: Alternative1<Either<string>> = {
  ...Alternative,
  orElse: (fa, fb) => {
    return fa.fold(
      () => fb,
      () => fa
    )
  }
}

In this implementation, we define a new eitherAlternative object that extends the existing Alternative instance. We override the orElse operation to handle Either values by folding over them. If the first computation (fa) is a Left, we return the second computation (fb). Otherwise, we return the first computation (fa).

With this custom Alternative instance, we can now use the orElse operation on Either values:

const combinedComputation: Either<string, number> = eitherAlternative.orElse(computation1, computation2)

This allows us to combine computations that may result in an Either value using the Alternative typeclass.

It's worth noting that this missing Alternative instance for Either has been identified as an issue in the Effect-TS ecosystem. The maintainers are actively working on adding this instance in a future release. In the meantime, you can use the workaround described above to define your own Alternative instance for Either.

In conclusion, while there is currently a missing Alternative instance for the Either type in the Effect-TS ecosystem, you can define your own instance to work around this limitation. The example implementation provided in this article demonstrates how to define a custom Alternative instance for Either and use it to combine computations that may result in an Either value.

Discord thread

https://discord.com/channels/795981131316985866/1154676286204162149

gcanti added a commit that referenced this issue Sep 22, 2023
@gcanti gcanti self-assigned this Sep 22, 2023
@gcanti gcanti closed this as completed in c186123 Sep 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants