dart-lang / dart-lang/language

[Static extensions] Should we allow static extensions to be accessed on any named type?

Open
#4,055 1 comment 0 reactions 0 assignees View on GitHub
static-extensions
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

The current proposal (#3835) for static extensions only allows static members and constructors to be accessed only on "class-like" types (that is classes, mixin classes, mixins, enums, and extension types). In principle, we could relax this restriction, either in the case that the `on` type is a typedef (see #4052) or in the case that the receiver is a typedef name. For example, we could allow:

```dart
typedef Pair = (T, T);

extension E on Pair {
factory Pair.twice(T x) => (x, x);
}
void test() {
var (x, y) = Pair.twice(3);
assert(3 == y);
assert(3 == x);
}
```

If so, do we attach the extension to the name `Pair` or to the underlying type? That is, does the following work?

```dart
typedef Pair = (T, T);

extension E on (T, T) {
factory Pair.twice(T x) => (x, x);
}
void test() {
var (x, y) = Pair.twice(3);
assert(3 == y);
assert(3 == x);
}
```

cc @dart-lang/language-team

Contributor guide

Open the contributing guide

Research direction

Start by reading the current static-extension proposal in issue #3835 and the typedef discussion in issue #4052. Compare the two typedef examples in this issue and determine whether extensions attach to the typedef name or its underlying type. Done means the language team has resolved the design question and documented the resulting rule.

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
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.