Skip to content

Instantly share code, notes, and snippets.

@devkabiir
Last active August 23, 2021 12:48
Show Gist options
  • Save devkabiir/18c3bed9818af5bc7bb9691aa03328ec to your computer and use it in GitHub Desktop.
Save devkabiir/18c3bed9818af5bc7bb9691aa03328ec to your computer and use it in GitHub Desktop.
dart_generics_issue.dart
mixin IModel {
int get field1;
}
mixin IWidget<Model extends IModel> {
///
Model get model;
}
mixin IState<W extends IWidget<M>, M extends IModel> {
W get widget;
// Access to model
M get model => widget.model;
void someMethod() {
assert(model.field1 != null);
print(model.runtimeType);
}
}
class MyModel with IModel {
@override
int field1 = 42;
}
class MyWidget with IWidget<MyModel> {
@override
final MyModel model;
MyWidget(this.model);
}
class MyState with IState<MyWidget, MyModel> {
MyState(this.widget);
@override
final MyWidget widget;
}
class IStateConsumer<M extends IModel, W extends IWidget<M>,
State extends IState<W, M>> {
IStateConsumer(this.state);
final State state;
}
void main() {
final consumer = IStateConsumer(MyState(MyWidget(MyModel())));
/// Works
if (consumer is! IStateConsumer) print('fails for no type parameters');
/// Works
if (consumer is! IStateConsumer<IModel, IWidget, MyState>)
print('fails when type parameters are incomplete');
/// Prints IStateConsumer<IModel, IWidget, MyState>
print(consumer.runtimeType);
/// Fails
if (consumer is! IStateConsumer<MyModel, MyWidget, MyState>)
print('fails when all type parameters are specific');
/// Works
final IStateConsumer<MyModel, MyWidget, MyState> implicitTypeParameters =
IStateConsumer(MyState(MyWidget(MyModel())));
/// Prints IStateConsumer<MyModel, MyWidget, MyState>
print(implicitTypeParameters.runtimeType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment