Skip to content

Iterable pattern #4498

@rrousselGit

Description

@rrousselGit

TL;DR: Dart lacks some way to do pattern matching on Iterable directly

When we use Iterables without transforming said Iterable into a List/Set/... (such as to avoid allocating a large collection), we end-up lacking a mean to use pattern matching.
Could we maybe have some Iterable-specific way to express a pattern match?

We could imagine #(a, b, c) as syntax for such pattern:

final list = [1, 2, 3];
final iterable = list.where((e) => e.isOdd);

switch (iterable) {
  case #(1, 3):
}

Or course, the token used could be anything else. Like #[], or not even use #.
Relying on () makes sense to me because Iterable.toString() uses (). But of course, this (1, 2) is taken by Record patterns.

Bonus:

  • Support #(1, 2, ...)
  • Support #(1, 2, ...final other) where other is an Iterable

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