dart-lang / dart-lang/language

Variadic Generics

Open
#1,774 15 comments 19 reactions 0 assignees View on GitHub
feature
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

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.