-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Access to parent generic type #54122
Comments
I see the signature: Future<R> callAsync<T extends AClass<R>, R>(T input) async and I can almost certainly guarantee that it won't work. (It might just type check in some cases, because the Without a context type, as your Would it be sufficient for you to write: Future<R> callAsync<R>(AClass<R> input) async {
// ...
} or do you need the Generally, this is not something that Dart type inference is able to solve for you. |
As @lrhn mentioned, the sky is the limit when it comes to inference of the type parameter However, @MuhammadEhsanMirzaei, you could use invariance (dart-lang/language#524) to get the effect that the desired type arguments are required. You'll have to write them manually because they aren't inferred (at this time), but at least you won't forget those type arguments, and you won't get them wrong (because that's a compile-time error). // NB: Needs `--enable-experiment=variance` in order to use the `inout` modifier.
abstract class AClass<inout T> {}
class BClass implements AClass<List<int>> {}
Future<T> exampleAsyncMethod<T>() async => <Never>[] as T;
Future<R> callAsync<T extends AClass<R>, R>(T input) async {
final result = await exampleAsyncMethod<R>();
return result; // result type is R
}
void main() async {
print(await callAsync<BClass, List<int>>(BClass()));
} With this adjustment, However, the above example relies on using a feature that hasn't been released at this time (and we can't promise a 100% that it will be released at all). So today you'd probably prefer to emulate invariance, which is possible in current Dart as follows: // --- Provide an invariant `AClass`
typedef Inv<X> = X Function(X);
typedef AClass<X> = _AClass<X, Inv<X>>;
abstract class _AClass<X, Invariance extends Inv<X>> {}
// --- End of invariance emulation.
class BClass implements AClass<List<int>> {}
Future<T> exampleAsyncMethod<T>() async => <Never>[] as T;
Future<R> callAsync<T extends AClass<R>, R>(T input) async {
final result = await exampleAsyncMethod<R>();
return result; // result type is R
}
void main() async {
print(await callAsync<BClass, List<int>>(BClass()));
} It may or may not be convenient (or even possible) for |
@lrhn @eernstg Thank you, just for acknowledging and Appreciate , I add I had planned to use it in the following package Thank you again for your help |
Dart SDK version: 3.2.0 (stable) on "macos_arm64"
I have these samples and think the 'dart' doesn't support this.
Current:
What I excepted:(*)
I can do this now:
But
can I except this(*) improvement in dart?
The text was updated successfully, but these errors were encountered: