-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Lowest common supertype in conditional expression #1857
Comments
It isn't clear to me how a lowest common supertype can be used to infer that (b ? 42 : null) should be of type int?. |
From a formal type-system point of view, it isn't quite right that Maybe, having to deal with both value types and reference types, it would be better to talk about "lowest common type-implicitly-convertible-to", because the actual criterion is implicit convertibility (therefore including |
Or |
I think my issue with understanding was being focused on the supertype thing, instead of implicit convertibility. Thanks for Clearing that up!
What about this case though? What type should result have? |
In my opinion the expression |
If you write |
@svick Dim x = {"hello", 3.14} |
That is the easiest way, but not the only one nor the cleanest. For instance you could choose a more specific type by picking the first of the calculated intersection. An effective run-time support for
Maybe the second solution is more elegant, because it does not clutter the method space with fake locals, but instead synthesizes a class (which is a common practice). For intersection-typed arrays the tecnique could be the same as the second solution (i.e. an array whose element type is the wrapper class), although I admit that I haven't meditated on it very much. Finally, I'd like to underline that LCS causes such trouble only in case of partially incompatible types, while nevertheless it represents an enrichment in expressivity when the conditional expression subexpressions already have a "simple" common convertible type. |
This is a dup of #1419. |
In a conditional expression
b ? x : y
, the last two operands determine the type of the whole expression. Essentially the procedure consists in taking the type ofx
and checking whethery
is implicitly convertible to it, or viceversa, otherwise a compile-time error occurs.This implies that the following expression does not compile:
even if it could (should) reasonably compile, with type
SystemException
, which is the lowest (most specific) common supertype of the type of the last two operands.The only way to have such expression compile is casting any of the two expressions to the desired supertype.
A lowest common supertype (LCS) always exists (at least
object
) and using the LCS to determine the type of a conditional expression is indeed a generalization of the current specification.This tecnique can be usefully applied to any kind of type, including nullable types, interfaces and dynamic types:
As the second example suggests, it's not always possible to infer a "simple" LCS. Anyway, the obtained intersection type would be just a compile-time compiler-level abstraction that does not need run-time support.
Briefly, among the properties of an expression typed as an intersection type
T & S
we have:T
orS
is expected;T
andS
--- conflicting signatures can be handled as usual.Being LCS an associative operation, it can be used to infer the type of an implicitly-typed array when no implicit conversion exists.
The text was updated successfully, but these errors were encountered: