dart-lang / dart-lang/language
Allow to resolve the inheritance signature conflict in derived class
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Dart has the implicit covariance for method overriding. So `String toString({int param = 1})` is a valid override to `String toString()`.
But this implicit covariance also changed the method signature, which might cause the conflict in derived class.
Check the code below for details:
# Example
Given the following code:
```dart
abstract class A {
@override
String toString({int param = 1}) => "A:$param";
}
mixin M {
String toString() => "M";
}
class B extends A with M {
}
```
The code doesn't compile due to the `M.toString()` isn't a valid override to `A.toString({int})`.
Check DartPad for a live example: https://dartpad.dev/6dd6d53c34592860688a51a7e2f1144e
Compiler complains:
```
lib/main.dart:10:7:
Error: Applying the mixin 'M' to 'A' introduces an erroneous override of 'toString'.
class B extends A with M {
^
lib/main.dart:7:10:
Info: The method 'M.toString' has fewer named arguments than those of overridden method 'A.toString'.
String toString() => "M";
^
lib/main.dart:3:10:
Info: This is the overridden method ('toString').
String toString({int param = 1}) => "A:$param";
^
Error: Compilation failed.
```
# Problem caused in Flutter
Flutter `Widget` class implements `Diagnosticable`, which have the declaration
```
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info})
```
So `Widget` can't include any mixin has `String toString()` defined.
# Proposal
Might not be a completely solution, but feel at least we should allow `B` to be capable to resolve the conflicts:
```dart
class B extends A with M {
// Given the conflict from `M` to `A`, hope language would allow this method in `B` to solve the conflict arbitrarily
@override
String toString({int param = 2}) => "B:$param";
}
```
Contributor guide
Research direction
Start with the DartPad example and the issue's inheritance and mixin rules to understand why the override conflict is rejected. Investigate the language specification and relevant analyzer or compiler handling, if located, then determine whether an explicit override in B can resolve the conflict without breaking override checks. Done means a decided language design with corresponding specification and implementation changes.
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
- Mostly clear
- Newbie friendliness
- 25/100