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
More specifically, I believe John's proposal is to allow non-constant initializer expressions for final non-static fields. Java and C# allow this and, as far as I know, it doesn't have any of the hairy implications that static initializations have. It's basically syntactic sugar for running an initializer expression at the beginning of the constructor body.
Always executed before any expression in the constructors' initializer list. 'this' must not be in scope for the expression. In addition, this prevents setting the final field in any other part of the code.
Sean: I think that your example will fail (compile-time error?), because in the initializer of 'b', you implicitly access 'this'. The access to 'c' in this initializer is actually an access to 'this.c', because the 'c' inside the class shadows outer 'c'.
I don't think that scoping rules are changed by this, and it would be really confusing if they were (as you note).
Activity
munificent commentedon Jan 4, 2012
More specifically, I believe John's proposal is to allow non-constant initializer expressions for final non-static fields. Java and C# allow this and, as far as I know, it doesn't have any of the hairy implications that static initializations have. It's basically syntactic sugar for running an initializer expression at the beginning of the constructor body.
DartBot commentedon Jan 4, 2012
This comment was originally written by [email protected]
Yes. Thanks for putting a finer point on it. :)
jtmcdole commentedon Jan 4, 2012
Added Area-Language, Triaged labels.
jtmcdole commentedon Jan 4, 2012
Set owner to @gbracha.
gbracha commentedon Jan 10, 2012
Added Accepted label.
gbracha commentedon Jan 24, 2012
We are looking at changes to initialization.
Removed Type-Defect label.
Added Type-Enhancement label.
DartBot commentedon Mar 26, 2012
This comment was originally written by [email protected]
Glad to see this is v.08. Thanks!
gbracha commentedon Mar 26, 2012
Don't thank us yet!
a. The change only applies to static variables. We're still thinking about the implications for instance variables. That's a lot harder.
b. As the spec notes, the changes aren't implemented yet. But they will be.
And thank you for bearing with us.
DartBot commentedon Mar 26, 2012
This comment was originally written by [email protected]
Excellent progress though. I still applaud it.
anders-sandholm commentedon May 8, 2012
Always executed before any expression in the constructors' initializer list. 'this' must not be in scope for the expression. In addition, this prevents setting the final field in any other part of the code.
Added this to the M1 milestone.
gbracha commentedon Jun 22, 2012
Spec version 0.11 allows arbitrary code in an instance initializer, provided "this" is not used.
Added Done label.
DartBot commentedon Jun 22, 2012
This comment was originally written by [email protected]
Excellent!
DartBot commentedon Jun 23, 2012
This comment was originally written by @seaneagan
in the following:
var c = 'c';
class A {
var c = 'd';
var b = c;
}
main() => print(new A().b);
I assume it will print 'c', but it could be surprising since normally instance variables can be accessed without "this." in class bodies.
DartBot commentedon Jun 23, 2012
This comment was originally written by [email protected]
Sean: I think that your example will fail (compile-time error?), because in the initializer of 'b', you implicitly access 'this'. The access to 'c' in this initializer is actually an access to 'this.c', because the 'c' inside the class shadows outer 'c'.
I don't think that scoping rules are changed by this, and it would be really confusing if they were (as you note).
gbracha commentedon Aug 29, 2012
Changed the title to: "Propose enhancement of field initializers".
[deps] rev async, boolean_selector, browser_launcher, cli_util, clock…
[deps] rev core, dartdoc, ecosystem, i18n, shelf, test, tools, web
Revert "[deps] rev core, dartdoc, ecosystem, i18n, shelf, test, tools…
[deps] rev core, ecosystem, i18n, shelf, tools, web