-
-
Notifications
You must be signed in to change notification settings - Fork 185
Implement typia.functional.match for compile-time pattern matching #1645
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: samchon <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Try again please.
Co-authored-by: samchon <[email protected]>
Co-authored-by: samchon <[email protected]>
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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!
Implements
typia.functional.match
for 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
otherwise
handler 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.