Skip to content
Kavachकवच

Syntax: Blocks are values

● Beginner3 min

A block { … } has a value: the value of its last line.

fn main() {
    const total = {
        const base = 100
        const tax = 18
        base + tax
    }
    print(total)
    const size = if total > 100 { "big" } else { "small" }
    print(size)
}

The program ran and printed:

kavach run block.kav

118big
block.kav: Kavach code, then what running it prints

total gets the value of the block's last line, base + tax, and base and tax are gone after it. An if is a value the same way, so it can pick between two values in one line.