Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use LazyArg for Effect.if branches #2451

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/three-poems-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"effect": minor
---

use LazyArg for Effect.if branches
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, what's the rationale behind this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2345


Instead of:

```ts
Effect.if(true, {
onTrue: Effect.succeed("true"),
onFalse: Effect.succeed("false"),
});
```

You should now write:

```ts
Effect.if(true, {
onTrue: () => Effect.succeed("true"),
onFalse: () => Effect.succeed("false"),
});
```
14 changes: 5 additions & 9 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3325,16 +3325,12 @@ export const option: <A, E, R>(self: Effect<A, E, R>) => Effect<Option.Option<A>

const if_: {
<A1, E1, R1, A2, E2, R2>(
options: { readonly onTrue: Effect<A1, E1, R1>; readonly onFalse: Effect<A2, E2, R2> }
options: { readonly onTrue: LazyArg<Effect<A1, E1, R1>>; readonly onFalse: LazyArg<Effect<A2, E2, R2>> }
): <E = never, R = never>(self: boolean | Effect<boolean, E, R>) => Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
<A1, E1, R1, A2, E2, R2>(
self: boolean,
options: { readonly onTrue: Effect<A1, E1, R1>; readonly onFalse: Effect<A2, E2, R2> }
): Effect<A1 | A2, E1 | E2, R1 | R2>
<E, R, A1, E1, R1, A2, E2, R2>(
self: Effect<boolean, E, R>,
options: { readonly onTrue: Effect<A1, E1, R1>; readonly onFalse: Effect<A2, E2, R2> }
): Effect<A1 | A2, E | E1 | E2, R | R1 | R2>
<A1, E1, R1, A2, E2, R2, E = never, R = never>(
self: boolean | Effect<boolean, E, R>,
options: { readonly onTrue: LazyArg<Effect<A1, E1, R1>>; readonly onFalse: LazyArg<Effect<A2, E2, R2>> }
): Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
} = core.if_

export {
Expand Down
44 changes: 21 additions & 23 deletions packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,35 +925,33 @@ export const forEachSequentialDiscard: {
export const if_ = dual<
<A1, E1, R1, A2, E2, R2>(
options: {
readonly onTrue: Effect.Effect<A1, E1, R1>
readonly onFalse: Effect.Effect<A2, E2, R2>
readonly onTrue: LazyArg<Effect.Effect<A1, E1, R1>>
readonly onFalse: LazyArg<Effect.Effect<A2, E2, R2>>
}
) => <E = never, R = never>(
self: Effect.Effect<boolean, E, R> | boolean
) => Effect.Effect<A1 | A2, E | E1 | E2, R | R1 | R2>,
{
<A1, E1, R1, A2, E2, R2>(
self: boolean,
options: {
readonly onTrue: Effect.Effect<A1, E1, R1>
readonly onFalse: Effect.Effect<A2, E2, R2>
}
): Effect.Effect<A1 | A2, E1 | E2, R1 | R2>
<E, R, A1, E1, R1, A2, E2, R2>(
self: Effect.Effect<boolean, E, R>,
options: {
readonly onTrue: Effect.Effect<A1, E1, R1>
readonly onFalse: Effect.Effect<A2, E2, R2>
}
): Effect.Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
}
<A1, E1, R1, A2, E2, R2, E = never, R = never>(
self: Effect.Effect<boolean, E, R> | boolean,
options: {
readonly onTrue: LazyArg<Effect.Effect<A1, E1, R1>>
readonly onFalse: LazyArg<Effect.Effect<A2, E2, R2>>
}
) => Effect.Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
>(
(args) => typeof args[0] === "boolean" || isEffect(args[0]),
(self: boolean | Effect.Effect<unknown, unknown, unknown>, { onFalse, onTrue }: {
readonly onTrue: Effect.Effect<unknown, unknown, unknown>
readonly onFalse: Effect.Effect<unknown, unknown, unknown>
// eslint-disable-next-line no-extra-boolean-cast
}) => isEffect(self) ? flatMap(self, (b) => (b ? onTrue : onFalse)) : Boolean(self) ? onTrue : onFalse
<A1, E1, R1, A2, E2, R2, E = never, R = never>(
self: Effect.Effect<boolean, E, R> | boolean,
options: {
readonly onTrue: LazyArg<Effect.Effect<A1, E1, R1>>
readonly onFalse: LazyArg<Effect.Effect<A2, E2, R2>>
}
): Effect.Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R> =>
isEffect(self)
? flatMap(self, (b): Effect.Effect<A1 | A2, E1 | E2, R1 | R2> => (b ? options.onTrue() : options.onFalse()))
: self
? options.onTrue()
: options.onFalse()
)

/* @internal */
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ export const exists: {
core.matchEffect(
forEach(
elements,
(a, i) => core.if_(f(a, i), { onTrue: core.fail(_existsParFound), onFalse: core.unit }),
(a, i) => core.if_(f(a, i), { onTrue: () => core.fail(_existsParFound), onFalse: () => core.unit }),
options
),
{
Expand Down
12 changes: 6 additions & 6 deletions packages/effect/test/Effect/cause-rendering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe("Effect", () => {
const effect = Effect.withSpan("spanB")(
Effect.withSpan("spanA")(
Effect.if(Effect.sync(() => Math.random() > 1), {
onTrue: Effect.fail(new E2()),
onFalse: Effect.fail(err)
onTrue: () => Effect.fail(new E2()),
onFalse: () => Effect.fail(err)
})
)
).pipe(Effect.catchTag("E2", (e) => Effect.die(e)))
Expand All @@ -55,8 +55,8 @@ describe("Effect", () => {
const effect = Effect.withSpan("spanB")(
Effect.withSpan("spanA")(
Effect.if(Effect.sync(() => Math.random() > 1), {
onTrue: Effect.fail(new E2()),
onFalse: Effect.fail(new E1())
onTrue: () => Effect.fail(new E2()),
onFalse: () => Effect.fail(new E1())
})
)
).pipe(Effect.catchAll((e) => Effect.fail(e)))
Expand All @@ -76,8 +76,8 @@ describe("Effect", () => {
const effect = Effect.withSpan("spanB")(
Effect.withSpan("spanA")(
Effect.if(Effect.sync(() => Math.random() > 1), {
onTrue: Effect.fail(new E2()),
onFalse: Effect.fail(new E1())
onTrue: () => Effect.fail(new E2()),
onFalse: () => Effect.fail(new E1())
})
)
).pipe(Effect.catchTags({ E2: (e) => Effect.die(e) }))
Expand Down
8 changes: 4 additions & 4 deletions packages/effect/test/Effect/sequencing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ describe("Effect", () => {
const result = yield* $(
true,
Effect.if({
onTrue: Effect.succeed(true),
onFalse: Effect.succeed(false)
onTrue: () => Effect.succeed(true),
onFalse: () => Effect.succeed(false)
})
)
assert.isTrue(result)
Expand All @@ -82,8 +82,8 @@ describe("Effect", () => {
const result = yield* $(
Effect.succeed(false),
Effect.if({
onFalse: Effect.succeed(true),
onTrue: Effect.succeed(false)
onFalse: () => Effect.succeed(true),
onTrue: () => Effect.succeed(false)
})
)
assert.isTrue(result)
Expand Down