Skip to content

Should null short-circuiting short-circuit spreads? #330

@leafpetersen

Description

@leafpetersen

As part of the NNBD language feature, we will allow null-aware operators to "short-circuit" a certain amount of the their continuation. So for example, a?.b.c will evaluate to null if a evaluates to null, instead of throwing a null pointer exception when c is called on null.

Should we extend this behavior to spreads? And if so, what semantics should it have? That is, given:

  void test() {
    List<int>? l = null;
    print([...l?.map((x) => x+1)]);
  }

If short-circuiting does not propagate through spreads, this is a compile time error, because l?.map(...) evaluates to either an Iterable or null, and it is an error to spread a nullable type. So you would have to change the code to ...?l?.map((x) => x+1).

If we make null-short-circuiting propagate through spreads, then this code could be allowed.

There is a question as to what semantics to give it. Consider ...l?.map((x) => x+1).

  • The treatment of short-circuiting elsewhere is that the short-circuited operation is only performed in the non-null continuation, which would suggest this semantics:
    • if (l == null) null else ...l.map((x) => x+1)
  • An alternative is to do the following:
    • if (l == null) ...?null else ...l.map((x) => x+1))

The latter semantics seems likely to be more useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    nnbdNNBD related issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions