-
-
Notifications
You must be signed in to change notification settings - Fork 673
Infer array type from first element in auto context #1021
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
Conversation
Regarding
Plus a not-so-obvious:
|
Last commit now first resolves all elements to their common type, so something like this can be inferred as well: var arr = [1, 2, 3.0]; // f64[] var ref = new Ref();
var arr = [ref, null]; // (Ref | null)[] |
nice! This even better. But how more expensive this approach? |
It doesn't make any difference if the type is annotated, as it is the case for all of our existing code. If it isn't annotated, every element needs to be resolved, which is rather simple to do for arrays of literals, but can become costly if there are lots of non-literal elements. That's uncommon, though. One benchmarks could be a large array of integer literals, as in math code, comparing annotated vs. inferred. I'd expect up to 2x slowdown from first evaluating the elements before compiling them. |
btw I one possible optimization is check if type reached upper limit of subtypeing like we know |
The implementation currently steps up the types, so if there's an f64 at one place it will compile the next literal with an f64 context again yielding an f64. Perhaps a fast path doing nothing if |
Hmm, I see one disadvantage of common denominator approach. Usually when people explicitly write something like this |
I think this should produce |
Could you add test for this case? |
Found some rouge cases: let arr1 = [null, "a"]; // should compile but produce error
let arr2 = [null]; // should produce error but compile
let arr3 = [1, null]; // should produce error but compile for now |
Another potential improvement: // nester arrays
let arr1 = [[1], [2]]; // currently produce compiler error |
An initial implementation of inferring array literals in auto contexts from their first element, as proposed in #1007. Does not yet handle
null
s.