dart-lang / dart-lang/language
Extension type dispatch.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
(_This issue is motivated by the idea to have support for [augmented maps](https://en.wikipedia.org/wiki/Augmented_map) that are general and efficient. I'll be referring to simple trees to keep things simple._)
Consider, for example, a simple tree data structure with extra "augmented" data.
The tree might be big, so we want the augmented data to be represented in an efficient way.
- a third party has provided us with an augmentation based on extension types (as `ThirdPartyAug`), and with a function that expects a `Tree` where AUG needs to be a `ThirdPartyAug` (as `run`)
- another third party has provided us with the tree data structure (as `Tree`).
Here's what that might look like:
```dart
class Tree {
final Tree? left;
final Tree? right;
final AUG aug;
const Tree({
required this.left,
required this.right,
required this.aug,
});
}
void main() {
run(
tree: ... some big tree ...,
);
}
void run({
required Tree tree,
}) {
// ...
}
extension type const ThirdPartyAug(int sum) {}
```
If we wanted to make `run` support custom augmentations, then it seems like we would be forced not to bound AUG to ThirdPartyAug, but to provide the interface that we want explicitly, e.g.:
```dart
void main() {
run2(
tree: ... some big tree ...,
sum: (final a) => a.sum,
);
}
void run2({
required Tree tree,
required int Function(AUG) sum,
}) {
// ...
}
class FirstPartyAug {
final int sum;
final int other;
const FirstPartyAug({
required this.sum,
required this.other,
});
}
```
In short:
- does that mean that type parameters bounded to an extension type are "useless"? (_If the bound is the only type that it can ever be instantiated with, then why should the type parameter exist in the first place?_)
- do we have to choose between efficiency (`run`) and generality (`run2`)? I suspect that calling `sum` via a level of indirection would be much less efficient than calling it directly by giving AUG a bound, or can we be general and maximally efficient at the same time?
Contributor guide
Research direction
Start with the issue's Tree, ThirdPartyAug, run, and run2 examples, then review how extension-type bounds and generic dispatch are specified. Done means reaching a documented language-design decision on whether bounded extension types can support both custom augmentations and efficient dispatch.
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
- 25/100