Skip to content

[Dot shorthands]: Allow shorthand syntax for instance methods in iterablesΒ #4534

@liamdoka

Description

@liamdoka

This request focuses on Iterables and anonymous functions.
Ideally, I'd like to be able to access an instance method on a parameter of an anonymous function with a known type.

Probably better described by an example:

class A {
  final bool b; // some member or getter.
}

final aList = <A>[A(), A(), A()];

// currently
final aPropertyList = aList.map((a) => a.b).

// ideal
final aPropertyList = aList.map(.b);

The proposal reduces noise around naming the lambda parameters in cases where they're not referenced again.
Useful for when we don't "care" what the parameter is, because each one is transformed in the same way.


To incite further discussion, separate from the initial proposal, this may open the door to more complex evaluations which may be unintended.

Consider the following.

enum CompassPoint { north, south, east, west }

class Movement {
  final CompassPoint point;
  final int distance;
}

final movements = <Movement>[
  Movement(.north, 5),
  Movement(.east, 3),
  Movement(.north, 4),
];

// Current static proposal
final eastMovements = movements.where((movement) => movement.point case .east);

// Possible new syntax
final eastMovements = movements.where(.point == CompassPoint.east);

// or even more magical
/// in this scenario, the [case] keyword could limit the following context to enum members.
final northMovements = movements.where(.point case .north);

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