Control Flow

TLang provides if/else, for loops over lists and ranges, while loops, match, and early return. Both if and match have statement and expression forms.

if / else if / else

Statement form — bodies are blocks.

      if (n < 0) {
          return "negative"
      } else if (n == 0) {
          return "zero"
      } else {
          return "positive"
      }
    

Expression form — assigns directly to a let. The else branch is required.

      let label = if (n < 0) "negative" else if (n == 0) "zero" else "positive"
      return if (ok) "done" else "failed"
    

for-in — List Iteration

Iterate over every element of a List. The loop variable is available only inside the body.

      for (item in items) {
          Terminal.println(item)
      }
    

Accumulate into a variable declared before the loop — rebinding updates the outer binding.

      let result = ""
      for (item in items) {
          let result = result + item + ", "
      }
      return result
    

Iterate over model keys.

      use TLang.Leaf

      let model = Leaf.model()
      let keys  = Leaf.keys(model)
      for (k in keys) {
          let entry = Leaf.get(model, k)
          // use entry...
      }
    

for — Range Iteration

Iterate over an integer range.

      // Inclusive on both ends: 1 2 3 4 5
      for (i in 1..5) {
          Terminal.println(i)
      }
    

Range with to / until keywords.

      for (i 1 to 5)    { /* 1 2 3 4 5 — inclusive */ }
      for (i 0 until 5) { /* 0 1 2 3 4 — exclusive */ }
    

Compute the sum of 1 to n.

      func sumTo(n: Int): Int {
          let total = 0
          for (i in 1..n) {
              let total = total + i
          }
          return total
      }
    

while

Loop while a condition is true.

      let i = 0
      while (i < 10) {
          Terminal.println(i)
          let i = i + 1
      }
    

match

Statement form — cases execute blocks.

      match (status) {
          case 200 { Terminal.println("OK") }
          case 404 { Terminal.println("Not found") }
          case 500 { Terminal.println("Server error") }
          default  { Terminal.println("Unknown: " + status) }
      }
    

Expression form — cases produce values. default is required.

      let mime = match (ext) {
          case "html" => "text/html"
          case "json" => "application/json"
          case "css"  => "text/css"
          default     => "application/octet-stream"
      }
    

Match on String values.

      let indent = match (lang) {
          case "python" => "    "
          case "yaml"   => "  "
          default       => "\t"
      }
    

return

Returns a value from a function immediately, regardless of nesting depth.

      func firstPositive(items: List): Int {
          for (n in items) {
              if (n > 0) {
                  return n
              }
          }
          return -1
      }
    

Functions without an explicit return at the end return the last evaluated expression, but explicit return is preferred for clarity.