Skip to content

Can't assigning a constructor argument to a member variable and use it in the intializer list #2872

Closed
@terrylucas

Description

@terrylucas

class Font {
  final num size;
  final FontFamily family;
  final num weight;
  final num lineHeight;

  Font([num size, this.family, this.weight, num lineHeight]) :
    this.size = size,
    this.lineHeight = lineHeight == null ? (size * 1.2).round() : lineHeight;
}

I'd like the Font constructor to be:

  Font([this.size, this.family, this.weight, num lineHeight]) :
    this.lineHeight = lineHeight == null ? (size * 1.2).round() : lineHeight;

Where the this.size is assigned to the member variable then use size to compute this.lineHeight however, that isn't possible. Neither size or this.size can be used to access the instance variable in the initializer list. So I have to have a temporary for size then assign this.size = size then I can use size in the initializer list to compute this.lineHeight.

Activity

gbracha

gbracha commented on May 3, 2012

@gbracha
Contributor

The design is very careful to ensure that no access to 'this' is possible in the initializers, so we can guarantee that uninitialized fields are not accessed. THis is an important property, but sometimes it means you have to type a little bit more.


Removed Type-Defect label.
Added Type-Enhancement, WontFix labels.

added a commit that references this issue on Feb 17, 2021
added 2 commits that reference this issue on Mar 23, 2021
added a commit that references this issue on May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-not-plannedClosed as we don't intend to take action on the reported issuetype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kevmoo@terrylucas@gbracha

        Issue actions

          Can't assigning a constructor argument to a member variable and use it in the intializer list · Issue #2872 · dart-lang/sdk