8000 Type inference fails when inferring type parameter of type argument · Issue #33758 · dart-lang/sdk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Type inference fails when inferring type parameter of type argument #33758

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

Closed
itsjoeconway opened this issue Jul 4, 2018 · 2 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang 8000 /language).

Comments

@itsjoeconway
Copy link

If a type parameter has a type parameter itself (e.g., <T extends Iterable<U>, U>), the inner type parameter is not inferred (U).

dev.66. I've attached a repro. I expected the output of this program to be List<String> String, but it is List<String> dynamic.

void main() {
  final object = getFirstObject(["string"]);
}

U getFirstObject<T extends Iterable<U>, U>(T iterable) {
  print("$T $U");
  return iterable.first;
}
@lrhn lrhn added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Jul 5, 2018
@lrhn
Copy link
Member
lrhn commented Jul 5, 2018

The type inference solution of T=List<String> and U = dynamic is a correct inference for the program at hand. It's not necessarily the most precise solution, but it satisfies all the constraints that your program sets.

If you change final object = ... to final String object = ..., you will see that U is now inferred as String. That's because the only place U occurs in the signature of getFirstObject is in the return type, so in order to constraint the inferred type for U, you need to put a constraint on the return type.

I'm not sure this is actually an error, just not the result you expected. I'll leave it to the inference experts to say what this particular case should give. @leafpetersen ?

@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2025
@lrhn lrhn added the closed-stale Closed as the issue or PR is assumed stale label Apr 9, 2025
@eernstg eernstg reopened this Apr 9, 2025
@eernstg eernstg removed the closed-stale Closed as the issue or PR is assumed stale label Apr 9, 2025
@eernstg
Copy link
Member
eernstg commented Apr 9, 2025

The feature 'inference-using-bounds' which was added to the language as of Dart 3.7 provides the desired inference:

void main() {
  final object = getFirstObject(["string"]);
}

U getFirstObject<T extends Iterable<U>, U>(T iterable) {
  print("$T $U"); // Prints 'List<String> String'.
  return iterable.first;
}

@eernstg eernstg closed this as completed Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

3 participants
0