-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Polymorphic lambdas #41121
Labels
Duplicate
An existing issue was already created
Comments
perhaps we can define a new type to contrain calculated operation like this ? const sum = <T extends Addable>(a: T, b: T) => a + b;
type Addable = ? // define Add operation just use |
I think this is a use case for #27808 With the proposed syntax you could do: const sum = <T extends oneof(number, bigint, string)>(a: T, b: T) => a + b; |
Duplicate #17428 |
Oh sorry, I missed this already have discussion |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Search Terms
lambda, closure, inferring, implicit polymorphism, deduce, type
Suggestion
Even in non-Hindley–Milner type systems like Rust and C++ (c++14 or later) polymorphic lambdas are supported:
C++14:
Rust:
So my suggestion start support this for TyteScript as well:
Note
(a: number | string, b: number | string): number | string
is ambiguous and may allowsum(1, 'a')
orsum(1, 'b')
as well so perhaps better always infer lambda as generic with single type parameter:and fallback to specialisation when
a
andb
have different types. Or probably restrict parametric as single uniform type.Why this necessary?
First of all it's reduce verbosity. But explicit generic lambdas have a lot of problems. For example this:
Cause to parsing problems with JSX mode.
Also it produce error "Operator '+' cannot be applied to types 'T' and 'T'." even with constrain.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: