dart-lang / dart-lang/language

More flexibility for the `base` keyword

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

Description

## Proposal

Define `base` as follows:

> if `A` is a `base class`, then all subtypes of `A` must inherit from `A`.



All of the guarantees from the [Class modifiers documentation](https://dart.dev/language/class-modifiers#base) would still hold:

> - The base class constructor is called whenever an instance of a subtype of the class is created.
> - All implemented private members exist in subtypes.
> - A new implemented member in a base class does not break subtypes, since all subtypes inherit the new member.
> - This is true unless the subtype already declares a member with the same name and an incompatible signature.

There would only be 1 difference:

> You must mark any class which implements ~~or extends~~ a base class as `base`, `final`, or `sealed`.

Adding the `base` modifier to a subtype would only be required when the class is implemented within the same library. (Alternatively, I would be in favor of disallowing implementation within the same library altogether, though that would be a breaking syntax change.)


Making this change to the keyword would enable the following:

#### a.dart
```dart
base class A {}
```

#### b.dart
```dart
abstract interface class B extends A {
int get value;
}
```

#### c.dart
```dart
class C extends A implements B {
@override
int get value => 42;
}
```



The analyzer may need to traverse several steps up the class heirarchy to check whether a class declaration is valid, but it already does this with the current `base` modifier:

```dart
base class A extends Foo {}

base class B extends A {}

base class C extends B {}

base class D implements C {}
```

A `base` class can be implemented within its own library, so whether `base class D implements C` is valid depends on whether `Foo` is a `base class`.

![base class](https://github.com/dart-lang/language/assets/10457200/f720f8ba-5ce8-47eb-9f1d-5105c9bfe014)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.