Skip to content

Commit

Permalink
refactor: strike and pattern to use overloads
Browse files Browse the repository at this point in the history
As nice as using the rest arguments would be, I decided to switch strike and pattern to use explicit overloads for up to 10 match arms.

this choice was made because  strike & pattern need to TELL match what type TIn is, to prevent having to explicitly tell each match arm what type it's matching on

 this change also simplifies the signatures of strike and pattern. While there are a lot, it should hopefully be easier to unpack.
  • Loading branch information
cakekindel committed Apr 19, 2020
1 parent c5e960c commit 4e43728
Show file tree
Hide file tree
Showing 18 changed files with 513 additions and 113 deletions.
4 changes: 3 additions & 1 deletion src/internal/common/types/ctor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* @description
* A reference to a `new`-able constructor
*
* @internal
*/
export interface Ctor<T> {
new (...args: unknown[]): T;
new (...args: any[]): T;
}
5 changes: 5 additions & 0 deletions src/internal/match-execution/types/any-executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {DefaultExecutor} from './default-executor';
import {MatchExecutor} from './match-executor';
import {UnwrapExecutor} from './unwrap-executor';

export type AnyExecutor<TIn, TOut> = MatchExecutor<TIn, TOut> | DefaultExecutor<TIn, TOut> | UnwrapExecutor;
3 changes: 1 addition & 2 deletions src/internal/match-execution/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @ignore
*/

export * from './any-executor';
export * from './default-executor';
export * from './infer-return-type';
export * from './infer-input-type';
export * from './match-executor';
15 changes: 0 additions & 15 deletions src/internal/match-execution/types/infer-input-type.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/internal/match-execution/types/infer-return-type.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/internal/match-execution/types/unwrap-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ import {Matched, TrackedEither} from '../../match-tracking/types';
*/
export interface UnwrapExecutor {
__exhaustive?: true;
(val: TrackedEither<unknown, unknown>): Matched<unknown>;
<TOut>(val: TrackedEither<unknown, TOut>): Matched<TOut>;
}
2 changes: 1 addition & 1 deletion src/internal/match-testing/match-ctor-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const matchCtorTester: MatchTester = <TIn, TOut>(
ctor: unknown,
transformer: MapFnOrValue<TIn, TOut>
) => {
if (isUnmatched(tracker) && isFn(ctor) && tracker.val instanceof ctor) {
if (isUnmatched(tracker) && isFn(ctor) && !!ctor.prototype && tracker.val instanceof ctor) {
return mapUnmatched(tracker, transformer);
}

Expand Down
172 changes: 172 additions & 0 deletions src/internal/overloads/pattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import {DefaultExecutor, MatchExecutor} from '../match-execution/types';
import {UnwrapExecutor} from '../match-execution/types/unwrap-executor';

/**
* @description
* Interface containing the function signatures for
* pattern, without default cases.
*
* @internal
*/
export interface PatternNonExhaustiveSigs {
<TIn, TOut>(arm0: MatchExecutor<TIn, TOut>): (val: TIn) => TIn | TOut;
<TIn, TOut>(arm0: MatchExecutor<TIn, TOut>, arm1: MatchExecutor<TIn, TOut>): (val: TIn) => TIn | TOut;
<TIn, TOut>(arm0: MatchExecutor<TIn, TOut>, arm1: MatchExecutor<TIn, TOut>, arm2: MatchExecutor<TIn, TOut>): (
val: TIn
) => TIn | TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>
): (val: TIn) => TIn | TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>
): (val: TIn) => TIn | TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>
): (val: TIn) => TIn | TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>
): (val: TIn) => TIn | TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>,
arm7: MatchExecutor<TIn, TOut>
): (val: TIn) => TIn | TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>,
arm7: MatchExecutor<TIn, TOut>,
arm8: MatchExecutor<TIn, TOut>
): (val: TIn) => TIn | TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>,
arm7: MatchExecutor<TIn, TOut>,
arm8: MatchExecutor<TIn, TOut>,
arm9: MatchExecutor<TIn, TOut>
): (val: TIn) => TIn | TOut;
}

/**
* @description
* Interface containing the function signatures for
* strike, with default cases.
*
* @internal
*/
export interface PatternExhaustiveSigs {
<TIn, TOut>(arm0: MatchExecutor<TIn, TOut>, otherwise: DefaultExecutor<TIn> | UnwrapExecutor): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>,
arm7: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>,
arm7: MatchExecutor<TIn, TOut>,
arm8: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
<TIn, TOut>(
arm0: MatchExecutor<TIn, TOut>,
arm1: MatchExecutor<TIn, TOut>,
arm2: MatchExecutor<TIn, TOut>,
arm3: MatchExecutor<TIn, TOut>,
arm4: MatchExecutor<TIn, TOut>,
arm5: MatchExecutor<TIn, TOut>,
arm6: MatchExecutor<TIn, TOut>,
arm7: MatchExecutor<TIn, TOut>,
arm8: MatchExecutor<TIn, TOut>,
arm9: MatchExecutor<TIn, TOut>,
otherwise: DefaultExecutor<TIn> | UnwrapExecutor
): (val: TIn) => TOut;
}

0 comments on commit 4e43728

Please sign in to comment.