-
Notifications
You must be signed in to change notification settings - Fork 1.7k
FR: Add generic type <T> to Type or suggest alternative #30115
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
Comments
All in all, I have no good alternative right now. As a general rule, the Strong mode allows methods to have type parameters. That's more useful because type parameters can actually be used in class Provider<T>;
const factory Provider() = _ProviderUseClass<T, U>;
const factory Provider.useClass<U extends T>() = _ProviderUseClass<T, U>;
}
class _ProviderUseClass<T, U> implements Provider<T> {
...
} or abstract class Provider<T> {
const factory Provider = ProviderUSeClass<T, T>;
}
class ProviderUseClass<T, UseClass extends T> implements Provider<T> {
const ProviderUseClass();
} The former still requires us to allow type parameters on named constructors, so also not a solution yet, even in strong mode, and strong mode doesn't work in production either. The latter works, but requires the user to use a different class instead of an argument in the constructor. Another "solution" would be to not pass raw types, but require the user to wrap them in a class: const Provider<RpcService>(useClass: const UseClass<CachedRpcService>()); Heck, you could event let the The "function with return-type T" is also a known problem. Even with the new |
We've discussed both of these in the past. Here was my summary of the final discussion of
Not sure I understand this. |
After understanding how types work over the last year, I no longer consider my request valid :) |
Apologies and please close if this a duplicate, but I couldn't find it.
In AngularDart,
Type
is used both as a compile-time symbol/token for many operations, such as dependency injection. For example, here is a way a user might say "allow injecting RpcService, and create a new instance of CachedRpcService to satisfy the dependency":Unfortunately, there is no good way for me to use types to help users from making silly mistakes. For example, this is totally valid, and will appear to work until runtime:
I realize we could write our own lints+analyzer plugin+yada yada, but this seems like a legitimate place where a generic type argument to
Type
could give this to us for free. For example, imagine we did the following:I considered the alternative of constructor tear-offs, for example:
But I run into a similar issue, there is no way to statically say "a function with any number or type of arguments as long as it returns T". Any ideas?
The text was updated successfully, but these errors were encountered: