dart-lang / dart-lang/language

More compact definition of generic classes with type parameters that extend other generic classes

Open
#102 6 comments 13 reactions 0 assignees View on GitHub
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

When defining a generic class which has a generic parameter that should extend a type that in turn is a generic class, you now have to add all the generic parameters of the second type also in the parameter list of the first class to be able to access those type parameters. This is cumbersome and seems unnecessary.

For example, the following definition:

```dart
class A,U,V,W> {
U u;
}
```

could be simplified to:

```dart
class A> {
U u;
}
```

I don't think that allowing this syntax would have any implications or breaking changes, it would simply be syntactic sugar for the first syntax.

A concrete example is the following situation. Suppose you are using the BLoC pattern and have a base class for blocs `BaseBloc` which has a generic parameter that defines the state:

```dart
abstract class BaseBloc {
Stream get state;
}
```

and a base class for components in `AngularDart` (or widgets or `flutter`):

```dart
// in the current syntax the extra type parameter S is still necessary
class BaseBlocComponent/*,S*/> {
final T bloc;

S currentState;

BaseBlocComponent(this.bloc) {
bloc.stream.listen((state)=>currentState = state);
}

}
```

You could now implement a concrete state, bloc and component:

```dart

class MyState {}

class MyBloc extends BaseBloc {

}

class MyComponent extends BaseBlocComponent {
// The extra MyState parameter is unnecessary, because it is implied by the type of MyBloc
// Moreover, you need to know which classes MyBloc extends in order to be able to set the
// correct value for the second type parameter


}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.