Learn/Nim/2. Values and variables
Lesson 2 of 8
Sign in

Nim, lesson 2

Values and variables

Every value in Nim has a type, and the compiler checks types before your program runs. The basic ones you will use constantly:

Three ways to name a value

Nim has three keywords for declaring a name, and choosing the right one tells the reader (and the compiler) how the value will be used.

  • const is for values known before the program runs, like a tax rate. The compiler bakes them into the program.
  • let is for values computed while the program runs but never changed afterwards, such as a line you just read from input.
  • var is for values that change, such as a running total.

Prefer let and const, and reach for var only when you really need to change something. If you try to assign to a let, the compiler stops you, which catches a whole class of bugs.

2 exercises to try what you just read.

Exercise 1

Ticket total

Press Ctrl Enter or Check to test your code.

Exercise 2

Bank balance

Press Ctrl Enter or Check to test your code.

Types are inferred, or written out

Nim works out the type from the value, so let price = 12 makes an int. You can also write the type after a colon. That is required when you declare a var without a starting value:

Once a name has a type, it keeps it. total = "ten" is a compile error, because "ten" is a string, not an int.

Naming

Names use camelCase by convention: ticketPrice, maxSpeed. Nim has an unusual rule here: apart from the first letter, it ignores capitalisation and underscores when comparing names, so ticketPrice and ticket_price are the same name. Pick one style and stay consistent anyway.

Building text from values

The $ operator turns a value into a string, and & joins two strings:

For longer messages, the strformat module lets you put expressions in {} inside a string prefixed with fmt:

import lines go at the top of the file. std/ marks a module from Nim's standard library.

Further reading

  • Nim Tutorial (Part I): variables, constants and basic types
  • The strformat module
  • Nim Manual: identifier equality

A cinema sells tickets for 12 euros each, and every order pays a booking fee of 3 euros. The starter declares these values. For an order of 4 tickets, print:

Compute the total from the names instead of typing 51 yourself. The program reads no input.

Start a balance at 100. Print it, then add a deposit of 50 and print it, then take away a withdrawal of 30 and print it once more:

balance has to change, so declare it with var. The program reads no input.

42        # int: a whole number
3.75      # float: a number with a decimal point
"Ada"     # string: text, in double quotes
'A'       # char: exactly one character, in single quotes
true      # bool: true or false
const vatRate = 20        # fixed when the program is compiled
let name = "Ada"          # set once when the program runs, then never changed
var lives = 3             # can be changed later
lives = lives - 1
lives -= 1                # shorthand for the line above; += works too
let temperature: float = 21.5
var total: int     # starts at 0, the default for int
let cups = 3
echo "Cups: " & $cups      # Cups: 3
import std/strformat

let item = "coffee"
let cups = 3
echo fmt"{cups} cups of {item} cost {cups * 4} euros"
Tickets: 4
Total: 51
Start: 100
After deposit: 150
After withdrawal: 120