dart-lang / dart-lang/language

Declaring constructors and field type inheritance

Open
#4,178 2 comments 1 reaction 0 assignees View on GitHub
feature primary-constructors
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

@munificent has a magnificent write-up of declaring constructors: https://github.com/dart-lang/language/pull/4169

One thing it says is that a `this(final name)` introduces an instance variable with signature `final dynamic name;`.

If the same thing was written the "old-style way":
```dart
abstract class Bar {
abstract final String name;
}
class Foo implements Bar {
final name;
Foo(this.name);
}
```
the `final name` would be subject to interface type inheritance, giving Foo.name` the type `String`.

Should writing `Foo` as:
```dart
class Foo implements Bar {
this(final name);
}
```
also be subject to interface inheritance, and only fall back to `dynamic` if there is nothing to inherit (and an error if there are multiple super-interfaces whose combined super-interface doesn't have a unique most-specific type for `name`).

If not, a quick-fix to create a declaring constructor from a non-declaring one would have to insert the type. (Which it probably can.)
And quick-fixing in the other direction would have to insert `dynamic` on the field.

If it does inherit, then the two ways of writing the constructor are symmetric.
I *think* I prefer this, because I want to think of the parameters of `this` as *field declarations* that happen to be written inside a constructor. And as a field declaration, it does get interface inheritance.

(If we do go with no inheritance, could it at least default to `Object?` instead of `dynamic`. Then the user will figure out that they forgot to write the type. Or is that a job for the "no implicit dynamic" language feature, let's call it "Dart 4.0"!)

@dart-lang/language-team

Contributor guide

Open the contributing guide

Research direction

Start with the declaring-constructors write-up in dart-lang/language pull request 4169 and compare its treatment of `this(final name)` with the old-style `Foo implements Bar` example in this issue. Determine whether field type inheritance should apply, including conflicting super-interfaces and the fallback type; done means the language-design question has a documented decision.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.