Skip to content

lack of default operator #61

Closed
Closed
@DartBot

Description

@DartBot

This issue was originally filed by waq...@gmail.com


In Javascript, one could provide defaults for objects that might be null in the following fashion:

foo = bar || default;

foo would be assigned bar, unless bar was falsy (e.g. null), in which case foo was assigned default instead.

In Dart, the operator || is only useful with booleans (as all other objects evaluate to false) and I could find no replacement for null-checks on objects.

I propose an operator like ?|| or a new keyword like orElse, which evaluates the expression to its left, returns it if it is not null, or otherwise returns the expression to its right. If the expression to the left is not null, the expression to the right should not be evaluated.

foo = null orElse 1; // foo = 1
foo = 2 orElse neverEvaluated; // foo = 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-duplicateClosed in favor of an existing report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions