-
Notifications
You must be signed in to change notification settings - Fork 213
Make inference know and use the special rules for num operators #597
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
Comments
If we introduce something like case functions which is a more general mechanism with a similar typing structure as the The It makes sense to say that type inference should use
.. but is specified in terms of the member signatures, in order to elucidate the underlying structure. When the set of available typings for a given function does not have such a clear connection from the return type to the choice of a typing for the function, we might be able to use the types of actual arguments to make the choice. However, we probably don't want to run inference with all choices (for an expression containing invocations of multiple functions with such types: all combinations of choices) in order to select the best one. So it would be really nice if we could find a foundation for how to make this mechanism generalizable, rather than just piling special treatments on top of other special treatments. |
We need the "no implicit downcast" requirement (and or a combination of classees being abstract and sealed) before we can generally use a return-type/argument-type connection to enforce a type context on the argument. The reason it works for |
One thought: What if we had a "weak context type" or "context type hint" which could be applied to the static type analysis of an expression, but which does not require the expression to satisfy the type like a proper context type? The hint has no effect unless the expression uses the context type. |
Is this superseded by the update num rules that we landed? |
Yes, this is done now. |
We have special rules for the static typing of
num
operators, so that3 + 3
has static type int and3 + 3.0
has static type double.The type inference can use this for
var x = 1 + 2.0;
, but it can't use the constraint in the other direction. Example:This code looks like it should work. However, the
+
means that thenumbers.fold(0, sum)
occurs as the second operand ofint.operator+
which has static typenum
. Hence, the context type isnum
so the type argument tofold
becomesnum
(context type always wins), and thensum
is not a valid argument because it does not acceptnum
values as first argument.However, we know (or should know) that for
int + x
to beint
, the expected argument type isint
, notnum
, so we could infer a context type ofint
in this case.This works when the left operand has static type
int
, and the context type for the operation is eitherint
ordouble
, then the right operand must have the same type as the context type.So, likewise
double x = 2 + _;
could infer that_
must have typedouble
.(That might actually be a little confusing because then
int x = 0; double y = x + 1;
would work butdouble z = 1 + x;
would not, because of double literal coercion.)This applies to all four operations where there is a link between the more precise special-cased result type and one of the operand types:
int.+
,int.-
,int.*
, andint.%
.We should probably also special-case
int.remainder(...)
. I've hit issues when converting that to NNBD because the return type is alwaysnum
.This may become extra important after NNBD because we remove implicit downcasts, so expressions with context type
num
may no longer be used in places where anint
is needed. Not all situations will fail as statically as this one. For example, ifsum
had been written asthen
would currently be valid, but would stop being valid after NNBD because the context type of
num
would cause thesum
to be instantiated atnum
, and the result ofint + num
is not assignable toint
.This has already caused problems with code written while
+=
did not introduce the correct context type for the second operand. The code had to be rewritten when the type inference was fixed.@leafpetersen @eernstg @johnniwinther @stereotype441
The text was updated successfully, but these errors were encountered: