83 Control flow in result builders
Only the selected if branch contributes an element. The for loop contributes one element per iteration, preserving order in the final result.
norm
import std.build.BuildWith
import std.build.ResultBuilder
class Words implements ResultBuilder<String, String> {
private String text = ""
Void add(String value) { text = text + value }
String finish() { text }
}
String words(@BuildWith(Words.class) Function<String()> content) { content() }
main() {
printLine(words {
if true { "a" } else { "wrong" }
for letter : ["b", "c"] { letter }
})
}Expected output:
text
abcTry it: Make the condition false and predict the resulting text.
Precise rules: Reference.