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

Remove Effect.unified and Effect.unifiedFn in favour of Unify.unify #2158

Merged
merged 1 commit into from
Feb 15, 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
31 changes: 31 additions & 0 deletions .changeset/fair-scissors-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
"effect": minor
---

Remove Effect.unified and Effect.unifiedFn in favour of Unify.unify.

The `Unify` module fully replaces the need for specific unify functions, when before you did:

```ts
import { Effect } from "effect";

const effect = Effect.unified(
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
const effectFn = Effect.unifiedFn((n: number) =>
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
```

You can now do:

```ts
import { Effect, Unify } from "effect";

const effect = Unify.unify(
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
const effectFn = Unify.unify((n: number) =>
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
```
3 changes: 2 additions & 1 deletion packages/cli/src/internal/cliApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as HashMap from "effect/HashMap"
import * as Option from "effect/Option"
import { pipeArguments } from "effect/Pipeable"
import * as ReadonlyArray from "effect/ReadonlyArray"
import * as Unify from "effect/Unify"
import type * as BuiltInOptions from "../BuiltInOptions.js"
import type * as CliApp from "../CliApp.js"
import type * as CliConfig from "../CliConfig.js"
Expand Down Expand Up @@ -80,7 +81,7 @@ export const run = dual<
// Handle the command
return Effect.matchEffect(InternalCommand.parse(self.command, prefixedArgs, config), {
onFailure: (e) => Effect.zipRight(printDocs(e.error), Effect.fail(e)),
onSuccess: Effect.unifiedFn((directive) => {
onSuccess: Unify.unify((directive) => {
switch (directive._tag) {
case "UserDefined": {
return ReadonlyArray.matchLeft(directive.leftover, {
Expand Down
33 changes: 1 addition & 32 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type * as FiberRef from "./FiberRef.js"
import type * as FiberRefs from "./FiberRefs.js"
import type * as FiberRefsPatch from "./FiberRefsPatch.js"
import type { LazyArg } from "./Function.js"
import { dual, identity } from "./Function.js"
import { dual } from "./Function.js"
import type * as HashMap from "./HashMap.js"
import type * as HashSet from "./HashSet.js"
import type { TypeLambda } from "./HKT.js"
Expand Down Expand Up @@ -212,15 +212,6 @@ export declare namespace Effect {
readonly _E: Covariant<E>
readonly _R: Covariant<R>
}
/**
* @since 2.0.0
* @category models
*/
export type Unify<Ret extends Effect<any, any, any>> = Effect<
Success<Ret>,
Error<Ret>,
Context<Ret>
>
/**
* @since 2.0.0
* @category type-level
Expand Down Expand Up @@ -4656,28 +4647,6 @@ export const withMetric: {
<R, E, A extends In, Type, In, Out>(self: Effect<A, E, R>, metric: Metric.Metric<Type, In, Out>): Effect<A, E, R>
} = effect.withMetric

// -------------------------------------------------------------------------------------
// unify
// -------------------------------------------------------------------------------------

/**
* Used to unify functions that would otherwise return `Effect<A, B, C> | Effect<D, E, F>`
*
* @category unify
* @since 2.0.0
*/
export const unifiedFn: <Args extends ReadonlyArray<any>, Ret extends Effect<any, any, any>>(
f: (...args: Args) => Ret
) => (...args: Args) => Effect.Unify<Ret> = core.unified

/**
* Used to unify effects that would otherwise be `Effect<A, B, C> | Effect<D, E, F>`
*
* @category unify
* @since 2.0.0
*/
export const unified: <Ret extends Effect<any, any, any>>(f: Ret) => Effect.Unify<Ret> = identity

// -------------------------------------------------------------------------------------
// semaphore
// -------------------------------------------------------------------------------------
Expand Down
18 changes: 12 additions & 6 deletions packages/effect/src/internal/core-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ export const catchAllDefect = dual<
self: Effect.Effect<A, E, R>,
f: (defect: unknown) => Effect.Effect<A2, E2, R2>
) => Effect.Effect<A | A2, E | E2, R | R2>
>(2, (self, f) =>
>(2, <A, E, R, A2, E2, R2>(
self: Effect.Effect<A, E, R>,
f: (defect: unknown) => Effect.Effect<A2, E2, R2>
): Effect.Effect<A | A2, E | E2, R | R2> =>
core.catchAllCause(
self,
core.unified((cause) => {
(cause): Effect.Effect<A | A2, E | E2, R | R2> => {
const option = internalCause.find(cause, (_) => internalCause.isDieType(_) ? Option.some(_) : Option.none())
switch (option._tag) {
case "None": {
Expand All @@ -181,7 +184,7 @@ export const catchAllDefect = dual<
return f(option.value.defect)
}
}
})
}
))

/* @internal */
Expand Down Expand Up @@ -226,10 +229,13 @@ export const catchSomeDefect = dual<
) => Effect.Effect<A | A2, E | E2, R | R2>
>(
2,
(self, pf) =>
<A, E, R, A2, E2, R2>(
self: Effect.Effect<A, E, R>,
pf: (defect: unknown) => Option.Option<Effect.Effect<A2, E2, R2>>
): Effect.Effect<A | A2, E | E2, R | R2> =>
core.catchAllCause(
self,
core.unified((cause) => {
(cause): Effect.Effect<A | A2, E | E2, R | R2> => {
const option = internalCause.find(cause, (_) => internalCause.isDieType(_) ? Option.some(_) : Option.none())
switch (option._tag) {
case "None": {
Expand All @@ -240,7 +246,7 @@ export const catchSomeDefect = dual<
return optionEffect._tag === "Some" ? optionEffect.value : core.failCause(cause)
}
}
})
}
)
)

Expand Down
13 changes: 2 additions & 11 deletions packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,6 @@ export const catchAll: {
): Effect.Effect<A2 | A, E2, R2 | R> => matchEffect(self, { onFailure: f, onSuccess: succeed })
)

/**
* @macro identity
* @internal
*/
export const unified = <Args extends ReadonlyArray<any>, Ret extends Effect.Effect<any, any, any>>(
f: (...args: Args) => Ret
) =>
(...args: Args): Effect.Effect.Unify<Ret> => f(...args)

/* @internal */
export const catchIf: {
<E, EB extends E, A2, E2, R2>(
Expand Down Expand Up @@ -941,7 +932,7 @@ export const if_ = dual<
(self: boolean | Effect.Effect<unknown, unknown, unknown>, { onFalse, onTrue }: {
readonly onTrue: Effect.Effect<unknown, unknown, unknown>
readonly onFalse: Effect.Effect<unknown, unknown, unknown>
}) => typeof self === "boolean" ? (self ? onTrue : onFalse) : flatMap(self, unified((b) => (b ? onTrue : onFalse)))
}) => typeof self === "boolean" ? (self ? onTrue : onFalse) : flatMap(self, (b) => (b ? onTrue : onFalse))
)

/* @internal */
Expand Down Expand Up @@ -1051,7 +1042,7 @@ export const onError: {
} = dual(2, <A, E, R, X, R2>(
self: Effect.Effect<A, E, R>,
cleanup: (cause: Cause.Cause<E>) => Effect.Effect<X, never, R2>
): Effect.Effect<A, E, R2 | R> => onExit(self, unified((exit) => exitIsSuccess(exit) ? unit : cleanup(exit.i0))))
): Effect.Effect<A, E, R2 | R> => onExit(self, (exit) => exitIsSuccess(exit) ? unit : cleanup(exit.i0)))

/* @internal */
export const onExit: {
Expand Down
4 changes: 2 additions & 2 deletions packages/platform/test/Http/Multipart.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Multipart from "@effect/platform/Http/Multipart"
import { Chunk, Effect, identity, Stream } from "effect"
import { Chunk, Effect, identity, Stream, Unify } from "effect"
import { assert, describe, test } from "vitest"

describe("Multipart", () => {
Expand All @@ -15,7 +15,7 @@ describe("Multipart", () => {
Stream.fromReadableStream(() => response.body!, identity),
Stream.pipeThroughChannel(Multipart.makeChannel(Object.fromEntries(response.headers))),
Stream.mapEffect((part) => {
return Effect.unified(
return Unify.unify(
part._tag === "File" ?
Effect.zip(
Effect.succeed(part.name),
Expand Down
Loading