Skip to content

For brevity, allow redirecting factory constructors to omit the formal parameter list #4135

@eernstg

Description

@eernstg

This is a proposal that allows some constructor declarations to be less verbose.

The formal parameter list of a redirecting factory constructor has a certain amount of expressive power: It is possible to declare a stronger type constraint on each parameter than the corresponding parameter type in the redirectee. However, in the (very typical) case where the parameter types are the same, it is possible to compute the parameter list of this kind of constructor based solely on the redirectee.

This means that we could allow the formal parameter list to be omitted, in which case the formal parameter list would be implicitly induced, using the same parameter type for every parameter as in the redirectee.

The code would not be significantly harder to read: Documentation for a redirecting factory constructor that omits the formal parameter list could show the formal parameter list which is implicitly induced, and IDEs could show it when a mouse pointer is hovering over the constructor name.

Here is an example which is a variant of something that came out of discussions about .identifier shorthands (#357). The example is relevant simply because it declares several redirecting factory constructors.

abstract class EdgeInsetsGeometry {
  const EdgeInsetsGeometry();

  // Forward to EdgeInsets:
  const factory EdgeInsetsGeometry.fromLTRB(
    double left,
    double top,
    double right,
    double bottom,
  ) = EdgeInsets.fromLTRB;

  const factory EdgeInsetsGeometry.all(double value) = EdgeInsets.all;

  const factory EdgeInsetsGeometry.only({
    double left,
    double top,
    double right,
    double bottom,
  }) = EdgeInsets.only;

  const factory EdgeInsetsGeometry.symmetric({
    double vertical,
    double horizontal,
  }) = EdgeInsets.symmetric;

  static const zero = EdgeInsets.only();

  // Forward to EdgeInsetsDirectional:
  const factory EdgeInsetsGeometry.fromSTEB(
    double start,
    double top,
    double end,
    double bottom,
  ) = EdgeInsetsDirectional.fromSTEB;

  const factory EdgeInsetsGeometry.onlyDirectional({
    double start,
    double top,
    double end,
    double bottom,
  }) = EdgeInsetsDirectional.only;

  const factory EdgeInsetsGeometry.symmetricDirectional({
    required double horizontal,
    required double vertical,
  }) = EdgeInsetsDirectional.symmetric;

  const factory EdgeInsetsGeometry.allDirectional(double value) =
      EdgeInsetsDirectional.all;

  static const zeroDirectional = EdgeInsetsDirectional.only();
}

With the abbreviation which is proposed here, it would look as follows:

abstract class EdgeInsetsGeometry {
  const EdgeInsetsGeometry();

  // Forward to EdgeInsets:
  const factory EdgeInsetsGeometry.fromLTRB = EdgeInsets.fromLTRB;
  const factory EdgeInsetsGeometry.all = EdgeInsets.all;
  const factory EdgeInsetsGeometry.only = EdgeInsets.only;
  const factory EdgeInsetsGeometry.symmetric = EdgeInsets.symmetric;

  static const zero = EdgeInsets.only();

  // Forward to EdgeInsetsDirectional:
  const factory EdgeInsetsGeometry.fromSTEB = EdgeInsetsDirectional.fromSTEB;
  const factory EdgeInsetsGeometry.onlyDirectional = EdgeInsetsDirectional.only;
  const factory EdgeInsetsGeometry.symmetricDirectional = EdgeInsetsDirectional.symmetric;
  const factory EdgeInsetsGeometry.allDirectional = EdgeInsetsDirectional.all;

  static const zeroDirectional = EdgeInsetsDirectional.only();
}

Come to think of it, we could also allow the class name to be omitted. See #4144.

Metadata

Metadata

Assignees

No one assigned

    Labels

    brevityA feature whose purpose is to enable concise syntax, typically expressible already in a longer formenhanced-syntaxUsed with proposals about improvements of the Dart grammarsmall-featureA small feature which is relatively cheap to implement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions