Skip to content

Should spread collections support const? #63

Closed
@munificent

Description

@munificent

The current "Spread Collections" proposal does not allow using spread in a const list. This is necessary in cases where the object you are spreading could do unexpected computation, like:

class InfiniteSequence implements Iterable<int> {
  const InfiniteSequence();

  Iterator<int> get iterator {
    return () sync* {
      var i = 0;
      while (true) yield i ++;
    }();
  }
}

const forever = [...InfiniteSequence()];

But in cases where the object being spread is an actual core library List object, the spread is safe. That's because it must be const (since it's inside a const context), and we have access to the actual value at compile time, not just its static type.

So we could say that you can spread inside a const list if and only if the spread object const-evaluates to a dart:core List.

Is this worth doing? Personally, I dislike adding yet more special cases for const shenanigans, and I rarely use const, so I'm not inclined. But if users feel it's important, I can see doing it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhanced-constRequests or proposals about enhanced constant expressionsquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions