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.
| Form | Name | What it does | Example | Prints |
|---|---|---|---|---|
const name = value | Constant | Names a value that never changes | const city = "Pune" | Pune |
let name = value | Variable | Names a value that can change later | let count = 3 | 3 |
name = value | Change | Gives a let a new value | count = count + 1 | 4 |
name += value | Change in place | Short for count = count + 10 | count += 10 | 14 |
const name: Type = value | With a type | The same, with the type written out | const limit: Int = 10 | 10 |
const [a, b] = list | Take apart | Several names from one list, by position | const [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 1You 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
| Rule | Allowed | Not allowed |
|---|---|---|
| Letters, digits and _ | player_2 | player-2 |
| Can't start with a digit | place_2nd | 2nd_place |
| Any script | नाम | mixing scripts in one name (a warning) |
| snake_case for values | total_price | totalPrice |
| Not a keyword | matched | match |
| Any length | i, or a name of hundreds of characters | (there is no limit) |
| Once per block | the same name in an inner block | the same name twice in one block |
| Case counts | total 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 12A 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`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`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 scopefn main() {
const label = "outside"
if true {
const label = "inside"
print(label)
}
print(label)
}The program ran and printed:
kavach run scope.kav
insideoutside
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
One page for each form
Try it yourself
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
2What Kavach does for you
Check yourself
const price = 40
price = price + 2
print(price)