Skip to content

Syntax for handling nullable variables #607

Closed
@nathggns

Description

Hi guys.

Apple's Swift as a feature called "Optionals" that makes handling nullable variables a lot easier. While I don't expect an exact port of this feature (as Swift doesn't actually use a null value for nullable values, but instead it all works on top of their Optional enum) there are some syntax niceties that I think we could port. Specifically, their Optional chaining feature.

The syntax I would like is this:

// b is a maybe variable containing an object with a property of c with a type of number
let a = b?.c;

which would equivalent to the flow code:

// b is a maybe variable containing an object with a property of c with a type of number
let a: ?number;
if (b != null) {
    a = b.c;
}

You could chain this almost infinitely.

// d.e is a ?number
let a = b?.c?.d?.e;

which would be equivalent to the flow code

let a: ?number;
if (b != null && b.c != null && b.c.d != null) {
    a = b.c.d.e;
}

I apologise if this has been suggested before.

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