Skip to content

Nullifying Conditional Operator #4252

Open
@Tech-Tac

Description

@Tech-Tac

This proposal introduces a new syntax feature, the Nullifying Conditional Operator, which can be considered a shorthand for conditional ternary expressions where the else branch is always null.

Syntax

(please note that the specific syntax of the operator is still being discussed, see the syntax candidates section, ? will be used as a placeholder in the following examples)

Written in the form a ? b where:

  • a is a Boolean expression.
  • b is returned if a is true.
  • Otherwise, if a is false, null is returned.

Example usage:

final bool hasDrawer = getHasDrawer();

return Scaffold(
  drawer: hasDrawer ? drawerWidget,
  // equivalent to drawer: hasDrawer ? drawerWidget : null,
  ...

Where in this example Scaffold would have a non-null drawer parameter if the boolean flag hasDrawer evaluates to true, or null otherwise.

Syntax candidates

  • ?:: found too confusing by many
  • ?&
  • ?!
  • ?

Use Cases

  • Avoids repetitive null returns in ternary expressions
  • Makes conditional expressions more concise
  • Keeps code readable without introducing unnecessary verbosity
  • Propagating a value based on a Boolean flag

Would love feedback from the community and the Dart team on feasibility and potential implementation!

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