Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Make foreach null-safe #574

@yloiseau

Description

@yloiseau

The foreach statement is just syntactic sugar which is expanded into for with an iterator. Therefore, if the iterable is null, the code fails with a NullPointerException.

I think it would be nice to make the loop handle null as an empty container.

Code such as

foreach elt in elements {
    workWith(elt)
}

would be expanded in

for (let it=elements?:iterator(), it isnt null and it: hasNext(), ) {
    let elt = it: next()
    workWith(elt)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions