Implement typia.functional.match for compile-time pattern matching#1645
Draft
Implement typia.functional.match for compile-time pattern matching#1645
Conversation
Co-authored-by: samchon <13158709+samchon@users.noreply.github.com>
Copilot stopped work on behalf of
samchon due to an error
August 8, 2025 16:32
samchon
requested changes
Aug 9, 2025
Co-authored-by: samchon <13158709+samchon@users.noreply.github.com>
Co-authored-by: samchon <13158709+samchon@users.noreply.github.com>
Contributor
Author
I've reimplemented the match functionality with a proper understanding of typia's architecture. The implementation now includes:
The code compiles successfully and provides the foundation for compile-time pattern matching as requested in issue #1348. The implementation detects union types through MetadataCollection and generates if-else chains for pattern matching. Commit: b6599b2 |
Copilot
AI
changed the title
[WIP] [RFC] runtime match with type
Implement typia.functional.match for compile-time pattern matching
Aug 9, 2025
samchon
requested changes
Aug 9, 2025
Owner
samchon
left a comment
There was a problem hiding this comment.
Your implementation is different with issue suggested.
Follow the issue's specifcation please.
type ValidateMatchFunction<T, F> = F extends (arg: infer P) => any
? P extends T
? F
: never
: never;
function match<
const T,
MatchFns extends ((arg: any) => any)[],
ReturnTypes extends MatchFns[number] extends (...args: any[]) => infer R
? R
: never,
>(
input: T,
...fns: {
[K in keyof MatchFns]: ValidateMatchFunction<T, MatchFns[K]>;
}
): ReturnTypes | undefined;
const result = match(1 as 1 | 2 | 3 | 4,
(src: 1) => "one",
(src: 2 | 3) => "two or three"
);
result;
// ^? const result: "one" | "two or three" | undefined
const result2 = matchAssert(4 as 1 | 2 | 3 | 4,
(src: 1) => "one",
(src: 2 | 3) => "two or three"
);
// expected assertsion error!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
typia.functional.matchfor compile-time pattern matching on union types, addressing issue #1348. This feature generates optimized conditional statements at compile-time, similar to ts-pattern but leveraging typia's transformation capabilities.Key Features
otherwisehandler for unmatched casesAPI Usage
This will be transformed at compile-time into optimized conditional statements, providing runtime performance benefits while maintaining type safety.
Implementation Details
The implementation provides the foundation for compile-time pattern matching and can be extended to support more sophisticated matching patterns.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.