Skip to content

Desire for a way to indicate to the type system that the return value can't be nullable if some arguments are null #1166

Closed
@Hixie

Description

@Hixie

Consider this method from dart:ui:

double? lerpDouble(num? a, num? b, double t) {
  if (a == null && b == null)
    return null;
  a ??= 0.0;
  b ??= 0.0;
  return a + (b - a) * t as double;
}

When used, it is very common to know for a fact that a and b are not null, e.g. because they are literals. It is unfortunate that one nonetheless has to ceremoniously accompany these calls with a trailing !, as in lerpDouble(0.0, 100.0, t)!.

It would be nice if there was a way to say that if a and b are known to be statically non-null, then the return value can also be guaranteed to be statically non-null, and the ! can be dropped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions