Skip to content

Improve inferring generic parameters #839

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 11 commits into from
Sep 14, 2019
Merged

Improve inferring generic parameters #839

merged 11 commits into from
Sep 14, 2019

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Sep 12, 2019

potentially fixes #219
potentially fixes #837

This makes use of the new resolver infrastructure to improve inferring generic types, like in

function test<T>(arr: T[]) { ... }
test(float32Array);
// resolving Array<f32>, inferring T = f32
function test<T,R>(fn: (a: T) => R) { ... }
test(someFunctionTakingF32ReturningF64);
// resolving (a: f32) => f64, inferring T = f32, R = f64

by first obtaining the type of the respective argument containing a type parameter and then matching it, including encapsulated type parameters, against the template.

Still feels a bit fragile as if I'd missed something, so if you spot an issue, let me know :)

@MaxGraey
Copy link
Member

MaxGraey commented Sep 12, 2019

Could this PR also potentially fix #837?

@dcodeIO
Copy link
Member Author

dcodeIO commented Sep 12, 2019

Appears to be a different issue caused by ordering, but this PR might help to solve it.

@MaxGraey
Copy link
Member

Will be great!

@dcodeIO
Copy link
Member Author

dcodeIO commented Sep 13, 2019

The other issue should be resolved as well with the last commit. Now infers all types first while updating to compatible types, and only then does another pass to do the actual resolving.

@MaxGraey
Copy link
Member

MaxGraey commented Sep 13, 2019

It will be great inferring type from argument as well. For example:
Current (with this PR as well) we should:

const arr: i32[] = [1,2,3];
arr.reduce<i32>(((prev: i32, current: i32): i32 => prev + current), 0); // but `0` already infer as `i32`

Will be great don't specify <i32> for T and infer it from last argument like:

arr.reduce(((prev: i32, current: i32): i32 => prev + current), 0);

// other example:
arr.reduce(((prev, current) => prev && current), false);
// infer as arr.reduce<bool>(((prev: i32, current: i32): bool => prev && current), false);

@dcodeIO
Copy link
Member Author

dcodeIO commented Sep 13, 2019

Last commit should fix issues with

arr.reduce(((prev: i32, current: i32): i32 => prev + current), 0);

inferring U <- i32 but I'm not sure about

arr.reduce(((prev, current) => prev && current != 0), false);

yet. What we would have to do there is to have a special case for function expressions without any types that just so happen to be fully specified by inferring other types, creating a new function signature with types that then becomes resolved - or something along those lines.

@dcodeIO
Copy link
Member Author

dcodeIO commented Sep 13, 2019

Wait,

arr.reduce(((prev, current) => prev && current != 0), false);

appears to compile now. I guess the other logic we have for function expressions without types can pick this up now since the code now uses compileCallDirect.

@MaxGraey
Copy link
Member

MaxGraey commented Sep 13, 2019

Great! Well done!

@dcodeIO dcodeIO merged commit 763c3c1 into master Sep 14, 2019
@dcodeIO dcodeIO deleted the infer-generic branch November 8, 2019 01:59
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.

Validation error when resolving generic with int & float type Cannot resolve generic function with array arguments
2 participants