dart-lang / dart-lang/language
Variadic Generics
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
lack of variadic generics (like variadic templates in C ++ / D / etc) provokes writing a lot of useless code.
example:
```dart
class Tuple2 {
// ...
}
class Tuple3 {
// ...
}
```
like C++ solution:
```C++
template
class Tuple {
// ...
}
```
I see two possible syntax options
first:
```dart
class Tuple<...T> {
// ...
}
```
second:
```dart
class Tuple {
// ...
}
```
the second option is better in my opinion
Here is a basic example of variadic generics usage:
```dart
class Tuple {
final T item;
Tuple(...T this.item);
List toList() => ...item;
T[comptimeIndex] getItem() => item[comptimeIndex];
dynamic getItem(int runtimeIndex) => item[runtimeIndex];
Tuple<...T> withItem(T[i] item) => Tuple(...item.sublist(0, i), item, ...item.sublist(i+1));
}
// Usage
void main() {
final tuple = Tuple(10, 'str', false);
final tuple2 = tuple.withItem<1>('str2');
print(tuple2.getItem<1>()); // output: 'str2'
}
```
Contributor guide
Research direction
Start with the issue's Tuple2/Tuple3 examples and the two proposed variadic-generic syntaxes. Determine the language-spec areas affected and whether the examples define sufficient semantics for declarations, construction, indexing, and type inference. Done requires an agreed design and corresponding specification changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100