Skip to content
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

Closed
5 tasks done
MaxGraey opened this issue Oct 15, 2020 · 5 comments
Closed
5 tasks done

Polymorphic lambdas #41121

MaxGraey opened this issue Oct 15, 2020 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@MaxGraey
Copy link

MaxGraey commented Oct 15, 2020

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:

auto sum = [](auto a, auto b) {
   return a + b;
};

sum(1, 2);

Rust:

let sum = |a, b| a + b;

sum(1, 2);

So my suggestion start support this for TyteScript as well:

const sum = (a, b) => a + b;   // no errors with noImplicitAny

const num = sum(1, 2);     
// `num` infer as number
// `sum` infer as:
//     `(a: number, b: number): number => a + b`

const str = sum('a', 'b'); 
// `str` infer as string
// `sum` type signature updated to:
//     `(a: number | string, b: number | string): number | string => a + b`

Note (a: number | string, b: number | string): number | string is ambiguous and may allow sum(1, 'a') or sum(1, 'b') as well so perhaps better always infer lambda as generic with single type parameter:

const sum = <T extends string | number>(a: T, b: T): T => a + b

and fallback to specialisation when a and b 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:

const sum = <T>(a: T, b: T) => a + b;
const sum = <T extends string | number>(a: T, b: T) => a + b;

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@eczn
Copy link

eczn commented Oct 16, 2020

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 type to define + operator ?

@AlCalzone
Copy link
Contributor

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;

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 16, 2020
@RyanCavanaugh
Copy link
Member

Duplicate #17428

@MaxGraey
Copy link
Author

Oh sorry, I missed this already have discussion

@typescript-bot
Copy link
Collaborator

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
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants