Skip to content
Kavachकवच

Variables

Also known as: variable constant const let var declare assignment type inference integer float boolean string interpolation format immutable mutable naming rules valid name identifier name length snake_case

● Beginner4 minBefore this: OutputSpec 1.4

A variable is a name for a value. const names one that never changes; let names one that can.

Every form, with what it givesEach example is followed by a print of the name it sets.
FormNameWhat it doesExamplePrints
const name = valueConstantNames a value that never changesconst city = "Pune"Pune
let name = valueVariableNames a value that can change laterlet count = 33
name = valueChangeGives a let a new valuecount = count + 14
name += valueChange in placeShort for count = count + 10count += 1014
const name: Type = valueWith a typeThe same, with the type written outconst limit: Int = 1010
const [a, b] = listTake apartSeveral names from one list, by positionconst [first, second] = [7, 8]8

A whole program

fn main() {
    const age = 31
    const price: Float = 4.99
    const name = "asha"
    const happy = true
    let count = 0
    count += 1
    print("{name} is {age}, happy: {happy}, {price}, count {count}")
}

The program ran and printed:

kavach run types.kav

asha is 31, happy: true, 4.99, count 1
types.kav: Kavach code, then what running it prints

You don't have to write a type: 31 makes age an Int, and "asha" makes name a String. Write one when you want to say it, as in price: Float.

Use const unless the value really must change. Then a reader knows at a glance which names can move.

Naming rules

RuleAllowedNot allowed
Letters, digits and _player_2player-2
Can't start with a digitplace_2nd2nd_place
Any scriptनामmixing scripts in one name (a warning)
snake_case for valuestotal_pricetotalPrice
Not a keywordmatchedmatch
Any lengthi, or a name of hundreds of characters(there is no limit)
Once per blockthe same name in an inner blockthe same name twice in one block
Case countstotal and Total are different names(a value's name is snake_case anyway)
fn main() {
    const player_2 = "Ravi"
    const नाम = "Asha"
    const _hidden = 7
    const total_price = 499
    const i = 1
    const the_number_of_orders_still_waiting_for_payment_at_the_end_of_the_day = 12
    print("{player_2} {नाम} {_hidden} {total_price} {i} {the_number_of_orders_still_waiting_for_payment_at_the_end_of_the_day}")
}

The program ran and printed:

kavach run names-ok.kav

Ravi Asha 7 499 1 12
names-ok.kav: Kavach code, then what running it prints

A name can be as short or as long as you like: the language sets no limit. Pick the shortest name that still says what the value is — i is fine for a loop counter, total_price for a price.

A name can't start with a digit:

fn main() {
    const 2nd_place = "Ravi"
    print(2nd_place)
}

The compiler refused the program:

kavach run digit.kav

error: a name can't start with a digit (spec 1.5): start it with a letter, as `place_2nd`
digit.kav: Kavach code, then what running it prints

A keyword can't be a name either, and the message suggests one:

fn main() {
    const match = 3
    print(match)
}

The compiler refused the program:

kavach run keyword.kav

error: `match` is a keyword, so it can't be a name (spec 1.5): choose another, such as `match_value`
keyword.kav: Kavach code, then what running it prints

Nor can it be declared twice in the same block. An inner block may reuse it, and the outer name comes back after:

fn main() {
    const total = 10
    const total = 20
    print(total)
}

The compiler refused the program:

kavach run twice.kav

error: `total` is already declared in this scope
twice.kav: Kavach code, then what running it prints
fn main() {
    const label = "outside"
    if true {
        const label = "inside"
        print(label)
    }
    print(label)
}

The program ran and printed:

kavach run scope.kav

insideoutside
scope.kav: Kavach code, then what running it prints

Names that start with _

A variable nobody reads gets a warning, so leftovers don't pile up. Start the name with _ to say it is unused on purpose, and the warning goes. _ on its own throws a value away: an item you skip, or a loop counter you don't need.

fn main() {
    let draft = "not used yet"
    let _later = "not used yet, on purpose"
    const [_, second] = [1, 2]
    for _ in 0..2 {
        print("round")
    }
    print(second)
}

The compiler warned, then the program ran:

kavach run underscore.kav

warning: `draft` is never used: remove it, or start its name with `_` if that is on purposeroundround2
underscore.kav: Kavach code, then what running it prints

One page for each form

Try it yourself

Make a let named count starting at 0, add 1 to it twice, and print it.
Show the answer
fn main() {
    let count = 0
    count += 1
    count += 1
    print(count)
}

The program ran and printed:

kavach run let.kav

2
let.kav: Kavach code, then what running it prints

What Kavach does for you

  • Unused names are flagged

    A variable nobody reads gets a warning; start its name with _ to say it is on purpose.

    spec 1.4

  • A value nobody reads is never computed

    Native code skips work whose result is unused and changes nothing you could see.

    spec 1.4

Check yourself

QUIZ 1 / 1pick one
What happens when you run this program?
const price = 40
price = price + 2
print(price)