Skip to content

Infer function expressions in matching contexts #514

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

Merged
merged 7 commits into from
Feb 27, 2019

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Feb 24, 2019

This PR

  1. legalizes omitting types on function expressions within function type contexts and
  2. legalizes omitting any number of arguments

As an example, the test looks like this:

function testOmitted(fn: (a: i32, b: i32) => i32): i32 {
  return fn(1, 2);
}
assert(testOmitted((a, b) => a + b) == 3);
assert(testOmitted(a => a) == 1);
assert(testOmitted(() => 42) == 42);

function testOmittedReturn1(): (a: i32, b: i32) => i32 {
  return (a, b) => a + b;
}
function testOmittedReturn2(): (a: i32, b: i32) => i32 {
  return a => a;
}
function testOmittedReturn3(): (a: i32, b: i32) => i32 {
  return () => 42
}
assert(testOmittedReturn1()(1, 2) == 3);
assert(testOmittedReturn2()(1, 2) == 1);
assert(testOmittedReturn3()(1, 2) == 42);

fixes #449

@dcodeIO dcodeIO changed the title Infer function expression Infer function expressions in matching contexts Feb 25, 2019
@dcodeIO
Copy link
Member Author

dcodeIO commented Feb 25, 2019

One remaining question is whether we should more strictly check the signature, i.e. changing

map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U>

etc. into

map<U>(callbackfn: (value: T, index?: i32, array?: Array<T>) => U): Array<U>

etc. and, in this example, requireing at least one parameter to be present, even though, technically, any number of parameters less than max works as long as the return type matches. Wdyt?

@MaxGraey
Copy link
Member

MaxGraey commented Feb 25, 2019

Hmm, I don't think this make sense. Usually all params in signatures like that should be always optional by default. So additional mark ? manually is unnecessary work I guess. This not present in ts declarations and this not present in other languages

@dcodeIO
Copy link
Member Author

dcodeIO commented Feb 25, 2019

Right, looks like the only place where the signature actually matters is where it is actually called (here: fn() inside of map). Let's do it the TS way then.

@MaxGraey
Copy link
Member

MaxGraey commented Feb 25, 2019

fully optional map's callback signature also possible in real scenario: arr.map(() => 0) (just got zero filled array with same length of arr)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't require all parameters of callback function for .map, .filter, etc.
2 participants