Build output text one line at a time.

build-text Writers and buffers collect pieces of text before the final result is used.

Build Text

title
BuildText.kt
Replay: real traced execution (multi-file project)
fun main() {
    val title = "Report"
    var output = ""
    output += "$title\n"
    output += "items=2\n"
    output += "done=yes"

    val lines = output.split("\n")
    val first = lines[0]
    val length = output.length

    println("first=$first")
    println("lines=${lines.size}")
    println("length=$length")
}
fun main() {
    val title = "Log"
    var output = ""
    output += "$title\n"
    output += "items=2\n"
    output += "done=yes"

    val lines = output.split("\n")
    val first = lines[0]
    val length = output.length

    println("first=$first")
    println("lines=${lines.size}")
    println("length=$length")
}
fun main() {
    val title = "Notes"
    var output = ""
    output += "$title\n"
    output += "items=2\n"
    output += "done=yes"

    val lines = output.split("\n")
    val first = lines[0]
    val length = output.length

    println("first=$first")
    println("lines=${lines.size}")
    println("length=$length")
}
  1. title ← Report, output ← (empty), lines ← [Report, items=2, done=yes]

    1fun main() {2    val title→ Report = "Report" //@title="Log", "Notes"3    var output→ (empty) = ""4    output→ Report
     += "$titleReport\n"5    output→ Report
    items=2
     += "items=2\n"6    output→ Report
    items=2
    done=yes += "done=yes"78    val lines→ [Report, items=2, done=yes] = outputReport
    items=2
    done=yes.split("\n")9    val first→ Report = lines[0]Report10    val length→ 23 = output.length231112    println("first=$firstReport")13    println("lines=${lines.size3}")14    println("length=$length23")15}
    outputfirst=Report
    lines=3
    length=23
  1. title ← Log, output ← (empty), lines ← [Log, items=2, done=yes]

    1fun main() {2    val title→ Log = "Log"3    var output→ (empty) = ""4    output→ Log
     += "$titleLog\n"5    output→ Log
    items=2
     += "items=2\n"6    output→ Log
    items=2
    done=yes += "done=yes"78    val lines→ [Log, items=2, done=yes] = outputLog
    items=2
    done=yes.split("\n")9    val first→ Log = lines[0]Log10    val length→ 20 = output.length201112    println("first=$firstLog")13    println("lines=${lines.size3}")14    println("length=$length20")15}
    outputfirst=Log
    lines=3
    length=20
  1. title ← Notes, output ← (empty), lines ← [Notes, items=2, done=yes]

    1fun main() {2    val title→ Notes = "Notes"3    var output→ (empty) = ""4    output→ Notes
     += "$titleNotes\n"5    output→ Notes
    items=2
     += "items=2\n"6    output→ Notes
    items=2
    done=yes += "done=yes"78    val lines→ [Notes, items=2, done=yes] = outputNotes
    items=2
    done=yes.split("\n")9    val first→ Notes = lines[0]Notes10    val length→ 22 = output.length221112    println("first=$firstNotes")13    println("lines=${lines.size3}")14    println("length=$length22")15}
    outputfirst=Notes
    lines=3
    length=22