--
You received this message because you are subscribed to the Google Groups "Dart Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to core-dev+u...@dartlang.org.
I was thinking (idle minds are dangerous) that the "default constructor" is very primitive, and maybe it can be
Yes please. Maintaining constructors for simple classes are just boilerplate.I would even like to go so far thatclass C {int foo;final int bar;int baz = 42;}would get the default constructorC({this.foo, this.bar, int baz}) : this.baz = baz ?? this.baz;
--
You received this message because you are subscribed to the Google Groups "Dart Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to core-dev+u...@dartlang.org.
If we remove the initialization at declaration this would work with both final and vars but I'm unsure about other implicationsIs there any problem with having an implicit body in default constructors?I see, it could be turned intobut that wouldn't work with finals, but you can't overwrite an initialized final anyway so that wouldn't work anyway.
C({this.foo, this.bar, int baz}) {
if (baz != null) {
this.baz = baz;
}
}
class C {
int foo;
final int bar;
int baz;
C({this.foo, this.bar, int baz}) : this.baz = baz ?? 42;
}
class Foo {
int pNum;
int _myNum;
void set myNum(int value) { _myNum = value; }
}
main() {
var foo = new Foo()
..pNum = 10
..myNum = 20;
// This feels much more awkward.
var bar = new Foo({pNum: 10})
..myNum 20;
}
--
I think it would be really useful to scrape the public packages and see how many classes could take advantage of this. Look for classes with a single generative constructor that just initializes fields.
class C {int one;final int two;final int three;int four;}
C(this.two, this.three, {this.one, this.four});
It might be hard to determine that a class has public fields that should not be settable in the constructor, but given that fields with no explicit initializer are implicitly initialized to null means that some existing code might be omitting an explicit initializer that this change would require in order to not change the semantics of that code. (On the other hand, it would be exactly as hard to write a tool to automatically convert existing code.)
- the order of the parameters depends on the order of the field declarations. This worries me more, since a refactoring that reorders your fields can change the semantics of your code. Maybe what we need are mandatory named arguments?
On Thu, Sep 17, 2015 at 10:27 AM, Brian Wilkerson <brianwi...@google.com> wrote:It might be hard to determine that a class has public fields that should not be settable in the constructor, but given that fields with no explicit initializer are implicitly initialized to null means that some existing code might be omitting an explicit initializer that this change would require in order to not change the semantics of that code. (On the other hand, it would be exactly as hard to write a tool to automatically convert existing code.)
Wouldn't it behave the same with Lasse's proposal? The optional named parameter would be omitted, defaulting to null, then the field would get initialized to that null value.
class C {String s;}
Yes, they're equivalent in terms of the semantics of the language, but not necessarily in terms of the semantics intended by the author. Today, if I writeclass C {String s;}I get a default constructor that does not allow 's' to be set. But under this proposal it could be set in the constructor. If I really don't want to allow that I have to have an explicit constructor. Maybe it's not important enough to worry about.
class C {String s;}
--
You received this message because you are subscribed to the Google Groups "Dart Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to core-dev+u...@dartlang.org.
But under this proposal it could be set in the constructor. If I really don't want to allow that I have to have an explicit constructor. Maybe it's not important enough to worry about.Brian
How about instead making the default constructor have parameters for all uninitialized fields.
Based on the wording above. Wouldn't the following also prevent S from being set in the default constructor?class C {String s = null;}
--
ClassName{member1: value1, member2: value2}
DIV {className: 'section', children: [
H1 {children: ['Title'],
Div {className: 'paragraph', chldren: [
'This is the contents"]}]}