23 Collection if elements
A collection literal can include an element only when a condition holds.
norm
main() {
Boolean includeExtra = false
List<String> steps = [
"read",
if (includeExtra) "inspect",
"run"
]
printLine(steps.size())
printLine(steps[1])
}Expected output:
text
2
runHere includeExtra is false, so inspect contributes no element at all; run becomes index one. This is different from an ordinary if expression that chooses one of two values.
Try it: Set the flag to true and predict the list size and index-one item.
Precise rules: Reference.