Skip to content

Commit

Permalink
allow branching .match
Browse files Browse the repository at this point in the history
  • Loading branch information
cevr committed Aug 2, 2023
1 parent 0ded560 commit 2144de5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-worms-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftld": patch
---

make .match branches a union for potentially unrelated types
8 changes: 4 additions & 4 deletions lib/src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { _value, _tag, TAGS } from "./internals";
import type { Result } from "./result";
import { UnwrapNoneError, identity, isResult } from "./utils";

type OptionMatcher<A, B> = {
type OptionMatcher<A, B, C> = {
None: () => B;
Some: (value: A) => B;
Some: (value: A) => C;
} & {};

export class Some<A> {
Expand Down Expand Up @@ -59,7 +59,7 @@ export class Some<A> {
/**
* Executes the appropriate function from the provided matcher based on the type of the Option.
*/
match<B>(cases: OptionMatcher<A, B>): B {
match<B, C>(cases: OptionMatcher<A, B, C>): B | C {
return cases.Some(this[_value]);
}

Expand Down Expand Up @@ -121,7 +121,7 @@ export class None<A> {
/**
* Executes the appropriate function from the provided matcher based on the type of the Option.
*/
match<B>(cases: OptionMatcher<never, B>): B {
match<B, C>(cases: OptionMatcher<never, B, C>): B | C {
return cases.None();
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { _value, _tag, TAGS } from "./internals";
import { UnknownError, identity, isOption } from "./utils";
import type { Option } from "./option";

type ResultMatcher<E, A, B> = {
type ResultMatcher<E, A, B, C> = {
Err: (value: E) => B;
Ok: (value: A) => B;
Ok: (value: A) => C;
} & {};

export class Ok<E, A> {
Expand Down Expand Up @@ -91,7 +91,7 @@ export class Ok<E, A> {
/**
* Matches the Result using provided functions and returns the result.
*/
match<B>(cases: ResultMatcher<E, A, B>): B {
match<B, C>(cases: ResultMatcher<E, A, B, C>): B | C {
return cases.Ok(this[_value]);
}

Expand Down Expand Up @@ -203,7 +203,7 @@ export class Err<E, A> {
/**
* Matches the Result using provided functions and returns the result.
*/
match<B>(cases: ResultMatcher<E, A, B>): B {
match<B, C>(cases: ResultMatcher<E, A, B, C>): B | C {
return cases.Err(this[_value]);
}

Expand Down
9 changes: 2 additions & 7 deletions lib/src/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,13 +891,7 @@ describe.concurrent("Task", () => {
it("should accumulate errors", async () => {
const values = [1, 2, 3, 4];
const tasks: SyncTask<SomeError | OtherError, number>[] = values.map(
(x) =>
x > 2
? Task.Err(new SomeError())
: Task.from(
() => x * 2,
() => new OtherError()
)
(x) => (x > 2 ? Task.Err(new SomeError()) : Task.Ok(x * 2))
);
const asyncTasks = tasks.map((task) =>
task.flatMap(async (x) => Result.Ok(x))
Expand All @@ -918,6 +912,7 @@ describe.concurrent("Task", () => {
expect(asyncTask.run()).toBeInstanceOf(Promise);
expect(result.isErr()).toBeTruthy();
expect(result.unwrapErr().length).toBe(2);
expect(result.unwrapErr()).toEqual([new SomeError(), new SomeError()]);
});

it("should accumulate errors when provided a record", async () => {
Expand Down
16 changes: 8 additions & 8 deletions lib/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export type AsyncTask<E, A> = {
/**
* Matches the Task's Result and executes a function based on its variant (Ok or Err).
*/
match<B>(cases: {
match<B, C>(cases: {
Ok: (a: A) => Promise<B>;
Err: (e: E) => Promise<B>;
}): Promise<B>;
match<B>(cases: { Ok: (a: A) => B; Err: (e: E) => B }): Promise<B>;
Err: (e: E) => Promise<C>;
}): Promise<B | C>;
match<B, C>(cases: { Ok: (a: A) => B; Err: (e: E) => C }): Promise<B | C>;

/**
* Returns the successful value or throws an error if the Task is Err.
Expand Down Expand Up @@ -226,11 +226,11 @@ export type SyncTask<E, A> = {
/**
* Matches the Task's Result and executes a function based on its variant (Ok or Err).
*/
match<B>(cases: {
match<B, C>(cases: {
Ok: (a: A) => Promise<B>;
Err: (e: E) => Promise<B>;
}): Promise<B>;
match<B>(cases: { Ok: (a: A) => B; Err: (e: E) => B }): B;
Err: (e: E) => Promise<C>;
}): Promise<B | C>;
match<B, C>(cases: { Ok: (a: A) => B; Err: (e: E) => C }): B | C;

/**
* Returns the successful value or throws an error if the Task is Err.
Expand Down

0 comments on commit 2144de5

Please sign in to comment.