golo-lang icon indicating copy to clipboard operation
golo-lang copied to clipboard

Make `foreach` null-safe

Open yloiseau opened this issue 5 years ago • 1 comments

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)
}

yloiseau avatar Nov 24 '20 14:11 yloiseau

Makes sense

jponge avatar Nov 24 '20 18:11 jponge