Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recursive extension type #3930

Open
mmcdon20 opened this issue Jun 24, 2024 · 1 comment
Open

Allow recursive extension type #3930

mmcdon20 opened this issue Jun 24, 2024 · 1 comment
Labels
feature Proposed language feature that solves one or more problems

Comments

@mmcdon20
Copy link

This is kind of an alternative to allow recursive typedef.


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

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

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

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

const LinkedList<int> list = LinkedList(1, LinkedList(2, LinkedList(3)));
const BinaryTree<String> 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:

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:

extension type Json(Map<String, Json> | List<Json> | String | num | bool | Null _)
    implements Map<String, Json> | List<Json> | String | num | bool | Null {}

The above definitions could potentially be improved further if allow extension type to implement Record and Function types, and/or implicit coercion through implicit constructors are also added to the language.

@mmcdon20 mmcdon20 added the feature Proposed language feature that solves one or more problems label Jun 24, 2024
@lrhn
Copy link
Member

lrhn commented Jun 24, 2024

It's not allowed because it's impossible to represent the extension-type erased representation type in the current Dart type system.
Dart would have to add recursive types to the type system before this would be possible.

So this is not an alternative to recursive type aliases, it's basically the same feature, and if we have recursive types in the type system anyway, we might as well allow them in general using recursive type aliases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
2 participants