Summary
The current for
-loop pattern in Verse requires explicitly testing a potentially-failing expression (e.g. CanFail[]
) inside the loop body:
for (Item : MyItems) {
if (Item.CanFail[]) {
# handle success for item
Print("Item is valid")
} else {
# handle failure for item
Print("Item is not valid")
}
}
Because the failure check lives one level deeper, the real loop logic is indented and harder to scan, especially when the success branch is lengthy. I propose allowing the failure context to be expressed directly on the loop header:
for (Item : Items, Item.CanFail[]) {
# handle success for item
Print("Item is valid")
} else {
# handle failure for item
Print("Item is not valid")
}
Placing the success and failure branches at the same syntactic level:
- eliminates repetitive
if (CanFail[]) { … } else { … }
boilerplate both inside and/or outside afor
-loop; - makes the loop’s control flow immediately obvious;
- preserves existing semantics - developers who prefer the old style don’t need to change anything to make the code work.
Please select what you are reporting on:
Verse
What Type of Bug are you experiencing?
Verse
Steps to Reproduce
N/A
Expected Result
N/A
Observed Result
N/A
Platform(s)
Verse VM
Additional Notes
I have made a more detailed description of this idea with some usage examples and comparisons between different syntaxes on the following forum post, feel free to read it for more detailed information about this feature request: