Reputation: 3547
Consider the following:
class ModularRoute<TPageParameters extends PageParameters,
TModularPage extends ModularPage<TPageParameters>>
extends BaseModularRoute<TPageParameters, TModularPage> {
ModularRoute({
required BaseModule module,
required String route,
required TModularPage Function(Map<String, String?> params) createPage,
FutureOr<bool> Function(
ModularHistory route,
ModularRouterDelegate delegate,
)?
guard,
bool overrideModuleGuard = false,
}) : super(
module: module,
route: route,
createPage: createPage,
guard: guard,
overrideModuleGuard: overrideModuleGuard,
);
To actually construct this, I have to define both parameters of the generic every time. I.e:
ModularRoute<SomePageParameter, SomeModularPage>(...);
Is there a way in Dart to define this in a compound fashion so that the parameter generic is defined by the ModularPage's generic type? I.e. I'd love to define it like this:
class ModularRoute<TModularPage extends ModularPage<TPageParameters extends PageParameters>> {
...
}
And be done.
That of course doesn't work, but I'm constantly running into this problem in Dart and it creates verbose code for no reason because the compiler should be able to easily infer the generic types involved and then use them.
Upvotes: 0
Views: 123
Reputation: 71683
No.
If your class depends on two type parameters, it needs to declare two independent type parameters. There is no way to implicitly pick out one of the types from the other. If it needs a name, you must make it an actual parameter.
Upvotes: 1