Skip to content

[Request] Better Generic Type Inference Support #1992

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
ragokan opened this issue Nov 26, 2021 · 2 comments
Closed

[Request] Better Generic Type Inference Support #1992

ragokan opened this issue Nov 26, 2021 · 2 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@ragokan
Copy link

ragokan commented Nov 26, 2021

Hello, what I want to suggest is a bettter support for generics. You know, sometimes Dart automatically infers the type from the variables, but in some cases, it can't, such as in Bloc/Cubits.

class CounterCubit extends Cubit<int> {
  CounterCubit() : super(0);
}

class Example<MyCubit extends Cubit<State>, State> {
  final MyCubit cubit;
  Example(this.cubit);
  State get state => cubit.state;
}

void main(List<String> args) async {
  final example = Example(CounterCubit());
  example.cubit; // it is CounterCubit
  example.state; // it is Object? or dynamic.
}

Lets say that we have a class like this. The type of example.cubit is inferred correctly, but example.state is not.

image
image

To infer the type correctly, we have to provide the type manuelly.

void main(List<String> args) async {
  final example = Example<CounterCubit, int>(CounterCubit());
  example.cubit; // it is CounterCubit
  example.state; // it is Object? or dynamic.
}
@ragokan ragokan added the feature Proposed language feature that solves one or more problems label Nov 26, 2021
@Levi-Lesches
Copy link

Levi-Lesches commented Nov 29, 2021

How would you want this to work? The code doesn't declare any base class for State, so if you omit it Dart has nothing to go off of. It's able to infer CounterCubit and not just Cubit because you declared MyCubit extends Cubit<State> and you passed in a CounterCubit as the parameter. But there's no equivalent "hint" for the type of State. In other words, I, as a human reader, have no idea that you want int because it's not actually in your code... until you manually put it in.

Unless you're asking that Dart recognize the link between MyCubit and State. You'd probably be interested in #620 and #102, which deal with the same request.

@ragokan
Copy link
Author

ragokan commented Nov 29, 2021

Yeah, I am asking about the link between CounterCubit and int. Let me close this one and discuss it on #620 and #102

How would you want this to work? The code doesn't declare any base class for State, so if you omit it Dart has nothing to go off of. It's able to infer CounterCubit and not just Cubit because you declared MyCubit extends Cubit<State> and you passed in a CounterCubit as the parameter. But there's no equivalent "hint" for the type of State. In other words, I, as a human reader, have no idea that you want int because it's not actually in your code... until you manually put it in.

Unless you're asking that Dart recognize the link between MyCubit and State. You'd probably be interested in #620 and #102, which deal with the same request.

@ragokan ragokan closed this as completed Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

2 participants