Skip to content

Private Named Parameters #4462

@munificent

Description

@munificent

This is the tracking issue for the Private Named Parameters proposal.

The proposal makes it easier to initialize and declare private instance fields using named constructor parameters. It addresses #2509 and turns code like this:

class House {
  int? _windows;
  int? _bedrooms;
  int? _swimmingPools;

  House({
    int? windows,
    int? bedrooms,
    int? swimmingPools,
  })  : _windows = windows,
        _bedrooms = bedrooms,
        _swimmingPools = swimmingPools;
}

Into this:

class House {
  int? _windows;
  int? _bedrooms;
  int? _swimmingPools;

  House({this._windows, this._bedrooms, this._swimmingPools});
}

The proposal harmonizes with (and in a couple of places mentions) the primary constructors proposal. When combined with that proposal, the above example becomes:

class House({
  var int? _windows,
  var int? _bedrooms,
  var int? _swimmingPools,
});

Without this proposal, the example here wouldn't be able to use a primary constructor at all without giving up either privacy on the fields or named parameters in the constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    brevityA feature whose purpose is to enable concise syntax, typically expressible already in a longer formsmall-featureA small feature which is relatively cheap to implement.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions