-
Notifications
You must be signed in to change notification settings - Fork 205
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
Downwards and upwards inference with object patterns #4180
Comments
I agree the behavior isn't ideal here. It's unfortunate, but I think I understand what's going on. Here's a simpler repro: class Box<T> {
final T value;
String get type => runtimeType.toString();
Box(this.value);
}
void main() {
final Box(:type) = Box(123);
print(type);
} This prints Type inference works in three phases: 1. First we calculate a context type schema for the pattern (here
|
I agree that that is the problem. It probably should instantiate with The current pattern behavior matches the current behavior of non-pattern declarations: |
Agreed! The only downside is the potential breakage (which is likely to be "good" breakage, by the way, because it is likely to cause some types to be more tight, e.g., replacing |
@nhannah commented on issue #3964. I'm going to move it to a separate issue here because I believe it's unrelated to the original topic of that issue. Their comment is:
I am interested if there would be a possibility to improve inference around this type alias object destructuring:
In this example
something
is typed asdynamic
as the generic type is lost.a
here is properly inferred as StudentT bool.In this example
something
is properly typed asbool
as it's explicitly set as the generic onStudentT
.This is where I get a slightly confused,
getStudent
can properly infer T as bool, but can't pass that inference to the destructuring. But ifgetStudent
does not infer it's type and has it set explicitly this all works fine.Swap to records and everything works as expected with no explicit types assigned but partial destructuring goes away:
The text was updated successfully, but these errors were encountered: