Skip to content

Proposal: operator !! (opposite of ??) #361

@MichaelRFairhurst

Description

@MichaelRFairhurst

Proposed solution to #360.

Where a ?? b is sugar for a != null ? a : b, there may be value in adding !! defined as a == null ? null : b.

Example: calling a function when the parameter is not null.

String? toPrint;
toPrint !! print(toPrint);

Example: awaiting a future when its not null

Future? fut;
fut !! await fut;

Example: asserting an object is fully initialized

assert(name !! age !! email !! password != null)

Similar to #219 except I think it has a few different advantages:

  • extremely simple and consistent with an existing well-used and understood dart concept
  • binary operator so left-to-right flow is easier to follow
  • wouldn't interact with any other syntax (prefix ? is was proposed to interact with maps, lists, optional parameters, etc.)

Disadvantages:

  • using ! and not throwing an exception on null is confusing. Maybe ?! or !??
  • Limited usefulness
  • repeat lhs required to take advantage of promotion

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problemsnull-aware-expressionsIssues proposing new expressions to manage null

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions