Skip to content

Commit 14c5711

Browse files
committedFeb 21, 2024
Remove Effect.unified and Effect.unifiedFn in favour of Unify.unify (#2158)
1 parent 489fcf3 commit 14c5711

File tree

6 files changed

+50
-52
lines changed

6 files changed

+50
-52
lines changed
 

‎.changeset/fair-scissors-compare.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
Remove Effect.unified and Effect.unifiedFn in favour of Unify.unify.
6+
7+
The `Unify` module fully replaces the need for specific unify functions, when before you did:
8+
9+
```ts
10+
import { Effect } from "effect";
11+
12+
const effect = Effect.unified(
13+
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
14+
);
15+
const effectFn = Effect.unifiedFn((n: number) =>
16+
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
17+
);
18+
```
19+
20+
You can now do:
21+
22+
```ts
23+
import { Effect, Unify } from "effect";
24+
25+
const effect = Unify.unify(
26+
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
27+
);
28+
const effectFn = Unify.unify((n: number) =>
29+
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
30+
);
31+
```

‎packages/cli/src/internal/cliApp.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as HashMap from "effect/HashMap"
88
import * as Option from "effect/Option"
99
import { pipeArguments } from "effect/Pipeable"
1010
import * as ReadonlyArray from "effect/ReadonlyArray"
11+
import * as Unify from "effect/Unify"
1112
import type * as BuiltInOptions from "../BuiltInOptions.js"
1213
import type * as CliApp from "../CliApp.js"
1314
import type * as CliConfig from "../CliConfig.js"
@@ -80,7 +81,7 @@ export const run = dual<
8081
// Handle the command
8182
return Effect.matchEffect(InternalCommand.parse(self.command, prefixedArgs, config), {
8283
onFailure: (e) => Effect.zipRight(printDocs(e.error), Effect.fail(e)),
83-
onSuccess: Effect.unifiedFn((directive) => {
84+
onSuccess: Unify.unify((directive) => {
8485
switch (directive._tag) {
8586
case "UserDefined": {
8687
return ReadonlyArray.matchLeft(directive.leftover, {

‎packages/effect/src/Effect.ts

+1-32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type * as FiberRef from "./FiberRef.js"
1919
import type * as FiberRefs from "./FiberRefs.js"
2020
import type * as FiberRefsPatch from "./FiberRefsPatch.js"
2121
import type { LazyArg } from "./Function.js"
22-
import { dual, identity } from "./Function.js"
22+
import { dual } from "./Function.js"
2323
import type * as HashMap from "./HashMap.js"
2424
import type * as HashSet from "./HashSet.js"
2525
import type { TypeLambda } from "./HKT.js"
@@ -212,15 +212,6 @@ export declare namespace Effect {
212212
readonly _E: Covariant<E>
213213
readonly _R: Covariant<R>
214214
}
215-
/**
216-
* @since 2.0.0
217-
* @category models
218-
*/
219-
export type Unify<Ret extends Effect<any, any, any>> = Effect<
220-
Success<Ret>,
221-
Error<Ret>,
222-
Context<Ret>
223-
>
224215
/**
225216
* @since 2.0.0
226217
* @category type-level
@@ -4617,28 +4608,6 @@ export const withMetric: {
46174608
<R, E, A extends In, Type, In, Out>(self: Effect<A, E, R>, metric: Metric.Metric<Type, In, Out>): Effect<A, E, R>
46184609
} = effect.withMetric
46194610

4620-
// -------------------------------------------------------------------------------------
4621-
// unify
4622-
// -------------------------------------------------------------------------------------
4623-
4624-
/**
4625-
* Used to unify functions that would otherwise return `Effect<A, B, C> | Effect<D, E, F>`
4626-
*
4627-
* @category unify
4628-
* @since 2.0.0
4629-
*/
4630-
export const unifiedFn: <Args extends ReadonlyArray<any>, Ret extends Effect<any, any, any>>(
4631-
f: (...args: Args) => Ret
4632-
) => (...args: Args) => Effect.Unify<Ret> = core.unified
4633-
4634-
/**
4635-
* Used to unify effects that would otherwise be `Effect<A, B, C> | Effect<D, E, F>`
4636-
*
4637-
* @category unify
4638-
* @since 2.0.0
4639-
*/
4640-
export const unified: <Ret extends Effect<any, any, any>>(f: Ret) => Effect.Unify<Ret> = identity
4641-
46424611
// -------------------------------------------------------------------------------------
46434612
// semaphore
46444613
// -------------------------------------------------------------------------------------

‎packages/effect/src/internal/core-effect.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,13 @@ export const catchAllDefect = dual<
147147
self: Effect.Effect<A, E, R>,
148148
f: (defect: unknown) => Effect.Effect<A2, E2, R2>
149149
) => Effect.Effect<A | A2, E | E2, R | R2>
150-
>(2, (self, f) =>
150+
>(2, <A, E, R, A2, E2, R2>(
151+
self: Effect.Effect<A, E, R>,
152+
f: (defect: unknown) => Effect.Effect<A2, E2, R2>
153+
): Effect.Effect<A | A2, E | E2, R | R2> =>
151154
core.catchAllCause(
152155
self,
153-
core.unified((cause) => {
156+
(cause): Effect.Effect<A | A2, E | E2, R | R2> => {
154157
const option = internalCause.find(cause, (_) => internalCause.isDieType(_) ? Option.some(_) : Option.none())
155158
switch (option._tag) {
156159
case "None": {
@@ -160,7 +163,7 @@ export const catchAllDefect = dual<
160163
return f(option.value.defect)
161164
}
162165
}
163-
})
166+
}
164167
))
165168

166169
/* @internal */
@@ -205,10 +208,13 @@ export const catchSomeDefect = dual<
205208
) => Effect.Effect<A | A2, E | E2, R | R2>
206209
>(
207210
2,
208-
(self, pf) =>
211+
<A, E, R, A2, E2, R2>(
212+
self: Effect.Effect<A, E, R>,
213+
pf: (defect: unknown) => Option.Option<Effect.Effect<A2, E2, R2>>
214+
): Effect.Effect<A | A2, E | E2, R | R2> =>
209215
core.catchAllCause(
210216
self,
211-
core.unified((cause) => {
217+
(cause): Effect.Effect<A | A2, E | E2, R | R2> => {
212218
const option = internalCause.find(cause, (_) => internalCause.isDieType(_) ? Option.some(_) : Option.none())
213219
switch (option._tag) {
214220
case "None": {
@@ -219,7 +225,7 @@ export const catchSomeDefect = dual<
219225
return optionEffect._tag === "Some" ? optionEffect.value : core.failCause(cause)
220226
}
221227
}
222-
})
228+
}
223229
)
224230
)
225231

‎packages/effect/src/internal/core.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,6 @@ export const catchAll: {
514514
): Effect.Effect<A2 | A, E2, R2 | R> => matchEffect(self, { onFailure: f, onSuccess: succeed })
515515
)
516516

517-
/**
518-
* @macro identity
519-
* @internal
520-
*/
521-
export const unified = <Args extends ReadonlyArray<any>, Ret extends Effect.Effect<any, any, any>>(
522-
f: (...args: Args) => Ret
523-
) =>
524-
(...args: Args): Effect.Effect.Unify<Ret> => f(...args)
525-
526517
/* @internal */
527518
export const catchIf: {
528519
<E, EB extends E, A2, E2, R2>(
@@ -925,7 +916,7 @@ export const if_ = dual<
925916
(self: boolean | Effect.Effect<unknown, unknown, unknown>, { onFalse, onTrue }: {
926917
readonly onTrue: Effect.Effect<unknown, unknown, unknown>
927918
readonly onFalse: Effect.Effect<unknown, unknown, unknown>
928-
}) => typeof self === "boolean" ? (self ? onTrue : onFalse) : flatMap(self, unified((b) => (b ? onTrue : onFalse)))
919+
}) => typeof self === "boolean" ? (self ? onTrue : onFalse) : flatMap(self, (b) => (b ? onTrue : onFalse))
929920
)
930921

931922
/* @internal */
@@ -1035,7 +1026,7 @@ export const onError: {
10351026
} = dual(2, <A, E, R, X, R2>(
10361027
self: Effect.Effect<A, E, R>,
10371028
cleanup: (cause: Cause.Cause<E>) => Effect.Effect<X, never, R2>
1038-
): Effect.Effect<A, E, R2 | R> => onExit(self, unified((exit) => exitIsSuccess(exit) ? unit : cleanup(exit.i0))))
1029+
): Effect.Effect<A, E, R2 | R> => onExit(self, (exit) => exitIsSuccess(exit) ? unit : cleanup(exit.i0)))
10391030

10401031
/* @internal */
10411032
export const onExit: {

‎packages/platform/test/Http/Multipart.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Multipart from "@effect/platform/Http/Multipart"
2-
import { Chunk, Effect, identity, Stream } from "effect"
2+
import { Chunk, Effect, identity, Stream, Unify } from "effect"
33
import { assert, describe, test } from "vitest"
44

55
describe("Multipart", () => {
@@ -15,7 +15,7 @@ describe("Multipart", () => {
1515
Stream.fromReadableStream(() => response.body!, identity),
1616
Stream.pipeThroughChannel(Multipart.makeChannel(Object.fromEntries(response.headers))),
1717
Stream.mapEffect((part) => {
18-
return Effect.unified(
18+
return Unify.unify(
1919
part._tag === "File" ?
2020
Effect.zip(
2121
Effect.succeed(part.name),

0 commit comments

Comments
 (0)
Please sign in to comment.