Skip to content

Propose enhancement of field initializers #1038

Closed

Description

@DartBot

This issue was originally filed by [email protected]


Propose that this:

class Foo{
final myOtherClass;

Foo()
: myOtherClass = new myOtherClass()

}

also allow this:

class Foo{
final myOtherClass = new myOtherClass();

Foo();
}

See: https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/32998e270e3cb57b/ec5cfe02c5acfee5#ec5cfe02c5acfee5

Activity

  1. munificent commented on Jan 4, 2012

    @munificent
    Member

    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.

  2. DartBot commented on Jan 4, 2012

    @DartBot
    Author

    This comment was originally written by [email protected]


    Yes. Thanks for putting a finer point on it. :)

  3. jtmcdole commented on Jan 4, 2012

    @jtmcdole
    Contributor

    Added Area-Language, Triaged labels.

  4. jtmcdole commented on Jan 4, 2012

    @jtmcdole
    Contributor

    Set owner to @gbracha.

  5. gbracha commented on Jan 10, 2012

    @gbracha
    Contributor

    Added Accepted label.

  6. gbracha commented on Jan 24, 2012

    @gbracha
    Contributor

    We are looking at changes to initialization.


    Removed Type-Defect label.
    Added Type-Enhancement label.

  7. DartBot commented on Mar 26, 2012

    @DartBot
    Author

    This comment was originally written by [email protected]


    Glad to see this is v.08. Thanks!

  8. gbracha commented on Mar 26, 2012

    @gbracha
    Contributor

    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.

  9. DartBot commented on Mar 26, 2012

    @DartBot
    Author

    This comment was originally written by [email protected]


    Excellent progress though. I still applaud it.

  10. anders-sandholm commented on May 8, 2012

    @anders-sandholm

    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.

  11. gbracha commented on Jun 22, 2012

    @gbracha
    Contributor

    Spec version 0.11 allows arbitrary code in an instance initializer, provided "this" is not used.


    Added Done label.

  12. DartBot commented on Jun 22, 2012

    @DartBot
    Author

    This comment was originally written by [email protected]


    Excellent!

  13. DartBot commented on Jun 23, 2012

    @DartBot
    Author

    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.

  14. DartBot commented on Jun 23, 2012

    @DartBot
    Author

    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).

  15. gbracha commented on Aug 29, 2012

    @gbracha
    Contributor

    Changed the title to: "Propose enhancement of field initializers".

  16. added this to the M1 milestone on Aug 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions