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

Make foreach null-safe #574

Open
yloiseau opened this issue Nov 24, 2020 · 1 comment
Open

Make foreach null-safe #574

yloiseau opened this issue Nov 24, 2020 · 1 comment

Comments

@yloiseau
Copy link
Contributor

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)
}
@jponge
Copy link
Contributor

jponge commented Nov 24, 2020

Makes sense

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants