You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider the following example (where T is some type, not an error):
voidmain() {
T value = ...;
if (value caseconst {}) ...;
switch (value) {
caseconst {}:break;
}
var x =switch (value) { caseconst {} =>1; default=>2};
}
Looking at the feature spec, I couldn't easily spot an answer to questions like these:
Does {} receive a context type schema (based on T, presumably), such that we can choose to make it a constant map respectively a constant set?
If no context type is provided then presumably {} would be a map, not a set? And {with, some, elements} would use bottom-up inference to choose the type arguments?
The text was updated successfully, but these errors were encountered:
The type inference of if (value case const{}) first finds the static type of value (T), then type checks the pattern using T as the matched value type.
This is the step which infers missing type arguments in the pattern.
Constant: Type check the pattern's value in context type M.
where M is the matched value type.
So, the constant expression is type checked/inferred with T as context type schema. If T provides a hint to being a set or map, and to the element/key/value types of such, those hints apply like they normally would, this is normal expression type inference with a context type. (And if those element/key/value types aren't constant, I assume we default to Never as usual, it's a normal constant expression.)
Basically, it's equivalent to:
T value = ...;
constT setOrMap = {};
if (value case setOrMap) ...;
except that the constant might not be required to have a static type assignable to T, since it's a refutable pattern.
For the switch case, the logic is the same. The matched value type for the case patterns is the static type of the switch value expression.
(Having a context type for set/map literals and int/double literals was part of the design considerations.)
OK, so we do have the required rules: Constant patterns receive a context type from the scrutinee. The information flow is simple: Constant patterns are refutable, so no information flows from the pattern to the scrutinee. Hence, it's not a problem that we need to get the type from the scrutinee before we can decide whether {} is a map or a set.
Consider the following example (where
T
is some type, not an error):Looking at the feature spec, I couldn't easily spot an answer to questions like these:
{}
receive a context type schema (based onT
, presumably), such that we can choose to make it a constant map respectively a constant set?{}
would be a map, not a set? And{with, some, elements}
would use bottom-up inference to choose the type arguments?The text was updated successfully, but these errors were encountered: