You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using private named fields in a constructor is very verbose as the type definitions and variable names need to be repeated and then the public variable name needs to be passed to the private variable:
You can avoid a lot of this by not using named arguments, but named arguments are a useful readability tool.
You could give up on private fields and just make them public, but private fields are useful for a variety of reasons:
You get null promotion on private attributes but not public ones
Paired with getters and setters, you can control external visibility for API readability and safety (fully private, read public write private, write public read private, side effects on write)
This is especially relevant in Flutter code where named constructor arguments are pretty standard practice for widgets and you'll often have nullable arguments that manage control flow in the build function:
if (_maybeBar ==null) returnSizedBox.shrink();
returnColumn(
children: [
// Relying on null promotion from the early return.BarView(bar: _maybeBar),
// More private field null promotion.if (_maybeFoo !=null) FooView(foo: _maybeFoo);
].
);
In practice. using private fields with named constructors is so tedious that Flutter developers typically just use public fields for their widgets and either lose null promotion (and rely on ! which is brittle to refactors), or jump through hoops with locally scoped variables in the build func to retain null promotion:
// Create a locally scoped variable to get null promotion.final bar = maybeBar;
if (bar ==null) returnSizedBox.shrink();
returnColumn(
children: [
// Relying on null promotion from the early return.BarView(bar: bar),
// Have to use a guard clause to get an if-scoped null promoted reference.if (maybeFoo casefinal foo?) FooView(foo: foo);
].
);
So, I'd like some way to have private named attributes with less boilerplate. I don't have strong preferences for how, but by far the simplest approach feels like just allowing it:
classMyClass {
constMyClass({
// Not possible today, you get an analysis error that named arguments can't start with `_`requiredthis._foo,
requiredthis._bar,
});
SomeLongType<SomeLongGeneric>? _foo;
String? _bar;
}
voidmain() {
// This is a little weird as now the private field name is exposed outside of the class,// but is that really so bad? Especially because it only happens when the dev// opts into it.// On some level I'd argue this is desirable-it communicates to the caller// that they're passing in a value that will henceforth be private.final myClass =MyClass(_foo:getFoo(), _bar:getBar());
}
Another approach might be some sort of shorthand syntactic sugar:
classMyClass {
constMyClass({
// `private` would be a keyword that tells the compiler that this is just a public proxy argument// for the correspondingly named private field. Probably we could come up with a less confusing// term to use for the keyword, this is just for sake of example.// This gets confusing if there are collisions with equivalently named public arguments and it feels// very "magical" so I'm not sure it's the best approach.required private this.foo,
required private this.bar,
});
SomeLongType<SomeLongGeneric>? _foo;
String? _bar;
}
voidmain() {
final myClass =MyClass(foo:getFoo(), bar:getBar());
}
Ideally, whatever solution we use would also be compatible with the potentially upcoming primary constructors:
// Implicitly declares and initializes `_foo` and `_bar` private fields.classMyClass({requiredSomeLongType<SomeLongGeneric>? _foo, requiredString? _bar});
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
Using private named fields in a constructor is very verbose as the type definitions and variable names need to be repeated and then the public variable name needs to be passed to the private variable:
You can avoid a lot of this by not using named arguments, but named arguments are a useful readability tool.
You could give up on private fields and just make them public, but private fields are useful for a variety of reasons:
This is especially relevant in Flutter code where named constructor arguments are pretty standard practice for widgets and you'll often have nullable arguments that manage control flow in the build function:
In practice. using private fields with named constructors is so tedious that Flutter developers typically just use public fields for their widgets and either lose null promotion (and rely on
!
which is brittle to refactors), or jump through hoops with locally scoped variables in the build func to retain null promotion:So, I'd like some way to have private named attributes with less boilerplate. I don't have strong preferences for how, but by far the simplest approach feels like just allowing it:
Another approach might be some sort of shorthand syntactic sugar:
Ideally, whatever solution we use would also be compatible with the potentially upcoming primary constructors:
The text was updated successfully, but these errors were encountered: