dart-lang / dart-lang/language

Allow recursive extension type

Open
#3,930 1 comment 2 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

This is kind of an alternative to [allow recursive typedef](https://github.com/dart-lang/language/issues/3714).

---

This would allow for recursive data structures backed by records such as in the following examples:

```dart
extension type const LinkedList._((T head, LinkedList? tail) _impl) {
const LinkedList(T head, [LinkedList? tail]) : _impl = (head, tail);
T get head => _impl.$1;
LinkedList? get tail => _impl.$2;
}

extension type const BinaryTree._(
({BinaryTree? left, BinaryTree? right, T value}) _impl) {
const BinaryTree(
{required T value, BinaryTree? left, BinaryTree? right})
: _impl = (left: left, right: right, value: value);

T get value => _impl.value;
BinaryTree? get left => _impl.left;
BinaryTree? get right => _impl.right;
}

const LinkedList list = LinkedList(1, LinkedList(2, LinkedList(3)));
const BinaryTree tree = BinaryTree(
value: 'A', left: BinaryTree(value: 'B'), right: BinaryTree(value: 'C'));
```
---

This would also allow for recursive `Function` definitions, such as in the following example:

```dart
extension type Church(Church Function(Church) _impl) {
Church call(Church f) => _impl(f);
}

final Church church = Church((f) => f);
```

---

If union types are also added, then you could potentially represent a Json type this way:

```dart
extension type Json(Map | List | String | num | bool | Null _)
implements Map | List | String | num | bool | Null {}
```

---

The above definitions could potentially be improved further if [allow extension type to implement Record and Function types](https://github.com/dart-lang/language/issues/3839), and/or [implicit coercion through implicit constructors](https://github.com/dart-lang/language/issues/3704) are also added to the language.

Contributor guide

Open the contributing guide

Research direction

Start by reading the linked proposal on recursive typedefs and the related issues about extension types implementing Record and Function types and implicit coercion. Compare their constraints with the LinkedList, BinaryTree, Church, and Json examples; the issue does not define specific files, tests, or a concrete completion criterion.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.