Skip to content

Dot Shorthand for Object DestructuringΒ #4567

@albinpk

Description

@albinpk

Summary

Introduce a shorthand for object destructuring where the type name can be omitted when it can be inferred from the right-hand side, using a leading dot (.).


Motivation

Object destructuring currently requires repeating the type name even when the matched expression already provides it, leading to unnecessary verbosity:

final Point(:x, :y) = Point(1, 2);

Proposal

Allow using a dot-prefixed object pattern whose type is inferred from the value being matched:

final .(:x, :y) = Point(1, 2);

This behaves the same as explicitly writing Point(:x, :y) but without repeating the type.


Examples

// current
final Size(:width, :height) = size;

// proposed
final .(:width, :height) = size;

// current
LayoutBuilder(
  builder: (context, constraints) {
    final BoxConstraints(:maxWidth, :maxHeight) = constraints;
    // ...
  },
);

// proposed
LayoutBuilder(
  builder: (context, constraints) {
    final .(:maxWidth, :maxHeight) = constraints;
    // ...
  },
);

// current
GestureDetector(
  onPanUpdate: (details) {
    final DragUpdateDetails(delta: Offset(:dx, :dy)) = details;
    // ...
  },
);

// proposed
GestureDetector(
  onPanUpdate: (details) {
    final .(delta: .(:dx, :dy)) = details;
    // ...
  },
);

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