Skip to content

Nesting Generics doesn't work as expected. #53327

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
eximius313 opened this issue Aug 23, 2023 · 4 comments
Closed

Nesting Generics doesn't work as expected. #53327

eximius313 opened this issue Aug 23, 2023 · 4 comments

Comments

@eximius313
Copy link

While having sucha a class with Generics is perfectly fine:

abstract class Response<TYPE extends Enum> {
  TYPE get type;
}

the compiler does not allow for "catching" the argument. Neither this:

class Processor<R extends Response<TYPE>> {
  TYPE handle(R response) => response.type;
}

nor this:

class Processor<R extends Response<TYPE extends Enum>> {
  TYPE handle(R response) => response.type;
}

works.


In your issue, please include:

  • Dart version and tooling diagnostic info (dart info) -> - Dart 3.0.0 (stable) (Thu May 4 01:11:00 2023 -0700) on "windows_x64"
  • Whether you are using Windows, macOS, or Linux (if applicable) -> Windows 10
  • Whether you are using Chrome, Safari, Firefox, Edge (if applicable) -> N/A
@julemand101
Copy link
Contributor

Would the following work for you?

class Processor<TYPE extends Enum, R extends Response<TYPE>> {
  TYPE handle(R response) => response.type;
}

@eximius313
Copy link
Author

Would the following work for you?

class Processor<TYPE extends Enum, R extends Response<TYPE>> {
  TYPE handle(R response) => response.type;
}

I know that this compiles, but this is a workaround. Processor should be able to take one param. The TYPE is nested in Response

@eernstg
Copy link
Member

eernstg commented Aug 24, 2023

Cf. dart-lang/language#620.

@lrhn
Copy link
Member

lrhn commented Aug 25, 2023

Correct, Dart does not allow you to nest generics, meaning declaring a new type variable inside the bound of another.
That would be nice, either as the language issue mentioned above, or as an extension to patterns (case List<var T>(...):, and treating type parameter bounds as type patterns: <E extends Foo<var T>>.

It's not a bug in the implementation, and I believe there are issues for the possible language changes, so closing this.

@lrhn lrhn closed this as completed Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants