Skip to content

Allow setting named private properties #2005

Open
@cedvdb

Description

@cedvdb

I find dart constructors initializer list often hinder readability and needlessly redundant when trying to have private members. They are also sometimes annoying to read in public api scenarios

I would like the ability to populate private members with named parameters without resorting to an initializer list.

What I propose is to have those 2 snippets be equivalent:

Currently:

class House {
  final int _windows;  
  final int _bedrooms;
  final int _swimmingPools;
  House.named({
    required int windows,
    required int bedrooms,
    required int swimmingPools,
  })  : _windows = windows,
        _bedrooms = bedrooms,
        _swimmingPools = swimmingPools
;}

Suggared:

class House {
  final int _windows;  
  final int _bedrooms;
  final int _swimmingPools;
  House.named({
    required this._windows,
    required this._bedrooms,
    required this._swimmingPools,
  });
}

In both cases you'd call House.named(windows: 2, bedrooms: 1, swimmingPools: 4);. The first snippet has needless noise in it.

I'm sure this was a design decision but I can't find an issue other than the closed one I posted above about this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions