Link Search Menu Expand Document


Table of contents

  1. Indentation
  2. Basic types
  3. Declaring variables
  4. Types of variables
  5. Some examples


Nim has a Python like code block structure, that means that we will use indentation to specify which statements are nested, but it’s more stringent in requiring spaces and disallowing tabs.

  you can put multiline comments between
  hash plus square brackets

var line = "@AT_1"

if line[0] == '@':
  echo "Sequence name: ", line[1 .. line.high]
  echo "Not a sequence name..."

Basic types

In the intentation example we introduced our first variable, called line, that is a string. These are the basic types in Nim that are automatically imported:

intInteger numbersvar threads = 8
floatNumbers with a decimal pointvar quasiPi = 3.15
charA single ASCII charactervar fastqHead = '@'
stringA string of text (multiple chars)var msg = "Hello"
boolTrue or Falsevar debug = false

There are more primitive types, but these are the easiest to use.

Declaring variables

In the example we used the var keyword to declare a variable:

var line = "@AT_1"  # this is a string

We created and initialized a variable of type string, and we didn’t need to specify the type as this is auto-inferred by the value. If we want to create a variable without initializing it, we must specify its value:

var line: string

We can declare and/or initialize multiple variables with a single var statement:

  line = "this line"
  delim = ','
  debug = true
  count : int

The last variable, count, is declared but had no value assigned to it.

:information: Identifiers in Nim are style insensitive. This is an interesting - and potentially confusing - feature that makes a variable called id_string to be callable as idString. Even if it was declared in “snake style” it can be accessed in “camelCase”.

Types of variables

Variable as very common in any scripting language, but their immutable siblings are less known:

Variable (it’s value can change)var
Immutable variable (cannot change)let
Constant (cannot change and must be computed at compile time)const

If you have a piece of information that will not need to change its value during the program execution, it’s wise to use let or const to store it. This will prevent unwanted behaviours and will provide some extra speed. You can use const only if the value can be known at compile time (for example, you know that FASTQ sequences start by @), while you’ll need to use let for immutable variables that will get their first value assigned at runtime (for example, a parameter supplied by the user).

:information: If you have a function returning a value, it can be used to assign a value to a constant. The function will be executed at compile time.

Some examples

Variables are supposed to change their value during the program execution:

# Declaring multiple variables, initializing them
  name = "Andrea"
  lang = "nim"

# Declaring uninitialized variables
  name: string
  age: int
  height: float

Immutable variables will contain constants or values that will not change during the program execution:

  pi = 3.1415
  progName = "Cottage Pi"
  progVer = "0.1.0"

We will see more about strings later, as well as more complex data structures ( arrays and sequences and tables ), but first it’s worth introducing some other basics concepts that will be very easy to pick coming from any other programming language: procedures (functions) and control flow (if, for…).