Skip to content
Kavachकवच

Variables: const

● Beginner2 min

const names a value that never changes after it is made.

fn main() {
    const name = "Asha"
    const limit: Int = 10
    print("{name} may borrow {limit} books")
}

The program ran and printed:

kavach run const.kav

Asha may borrow 10 books
const.kav: Kavach code, then what running it prints

Changing a const is refused before the program runs, with the fix in the message:

fn main() {
    const price = 40
    price = price + 2
    print(price)
}

The compiler refused the program:

kavach run let.kav

error: `price` is a `const`, so it can't change: declare it with `let`
let.kav: Kavach code, then what running it prints

What Kavach does for you

  • const unless it must change

    const values never change, and changing one is a compile error that says to use let. You see at a glance what can move.

    decision 0034