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
For sealed types with two implementations, Dart should be able to promote the type based on if-case statements, like it does with switch statements. Example given
Result<int,String> main(){
Result<int,String> result =okResult();
switch(result){
caseErr():return result;
caseOk():
}
int s = result.ok; // type promotion worksreturnOk(s+1);
}
But this does not
Result<int,String> main(){
Result<int,String> result =okResult();
if(result caseErr()){
return result;
}
int s = result.ok; // type promotion does not work herereturnOk(s +1);
}
The text was updated successfully, but these errors were encountered:
@eernstg based on our current chat here dart-lang/sdk#55104 , I assume the reason this works in the first instance is because specifying all cases of the sealed class triggers exhaustiveness analysis. While this is not currently done for if case statements?
Agree, we do not try to handle exhaustiveness with if except for union types, where we know that there are precisely two immediate subtypes.
We could do the same for sealed types with two immediate subclasses, but that's a feature which feels partial if it stops working at three subclasses.
We could aim to solve the general problem: Given a sealed type with subtypes T1...Tn, can we treat it as a union type T1|...|Tn and have type checks elimintate some parts of it.
(But then we run into the case of T4 being another sealed type, and wanting to eliminate that one subclass at a time. Possibly doable, likely not trivial.)
For sealed types with two implementations, Dart should be able to promote the type based on if-case statements, like it does with switch statements. Example given
This works
But this does not
The text was updated successfully, but these errors were encountered: