dart-lang / dart-lang/language
Do static declarations preclude anything?
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Thanks to @sgrekhov for bringing up this topic.
With extension types, we introduced the notion that one member declaration can preclude another member declaration. It is always a method that precludes a setter which is in the interface of a superinterface, or a setter that precludes a method which is in the interface of a superinterface (other pairs of kinds of declarations are not subject to preclusion).
For example:
```dart
extension type E1(int it) {
void m() {}
}
extension type E2(int it) implements E1 {
set m(_) {} // OK.
}
class C1 {
void m() {}
}
class C2 extends C1 {
set m(_) {} // Compile-time error.
}
```
If we consider classes (like `C1` and `C2` above) then we get a compile-time error because `C2` "has" a method named `m` and it "declares" a setter named `m=`, and that's an error.
However, when we consider extension types we say that the setter `E2.m=` _precludes_ the method `E1.m`, and there is no error. So we're basically ignoring the method-respectively-setter from the superinterface when it conflicts with a setter-respectively-method in the subtype.
(The motivation for preclusion is that extension types are allowed to _redeclare_ a name more freely than classes, mixins, etc. can _override_ a name, and we did not want to have a compile-time error for some specific situations involving redeclaration.)
However, the wording in the specification does not indicate that the subtype declaration must be an instance member declaration, which means that it should also work for static declarations:
```dart
extension type E1(int it) {
void set m() {}
}
extension type E2(int it) implements E1 {
static m(_) {} // OK?
}
```
In this case we would, arguably, again say that `E2.m=` precludes `E1.m`, and hence there is no error.
However, the implemented behavior is to flag this example as an error (which means that a static declaration _does not_ get to preclude anything).
I think we should adjust the specification to say that preclusion only applies when the given member declarations are instance member declarations, and static declarations are never the cause of preclusion. This makes sense conceptually as well, because the motivation for preclusion was based on the similarities and difference between overriding relations and redeclaration relations.
@dart-lang/language-team, WDYT?
Contributor guide
Research direction
Start by reading the extension-type preclusion wording in the Dart language specification and compare it with the implemented behavior described in the issue's examples. Confirm how static and instance declarations are handled, then update the specification so its wording matches the intended behavior and verify the examples remain consistent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers, documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100