Getting Started Nim Lang
Tags: programming, nim
Published: 2019-04-21 16:46:08

# Getting Started Nim Lang

Hi, I researched about programming languages for a weekend activity. In this article, I will not tell everything.

There are many programming languages in the tech world. I was undecided between Groovy and Nim languages.

Actually, I was looking for a programming language which like Python. Python's syntax pretty for me. Nim has a similar syntax.

When I started programming, I used many programming languages like Visual Basic 6, Pascal and ASP.

Nim has a syntax such as Delphi (Object Pascal). For example, a method could be created like that;

```nim
proc shopping(basket: Basket): int =
  if basket.id > 0:
    updateShoppingAmount(basket)

  saveShopping(basket)
```

You could use this method like that;

```nim
type
  Basket = ref object of RootObj
    id:* int
    amount:* float
    productName:* string
    userID: int


basket = Basket(id: 0, amount: 22.5, productName: 'Shoes')

shopping(basket)
```

BTW, types are like classes of the Nim language. In a C # class, private fields are created as like that;

```csharp
public class Basket
{
  private int userID { get; set; }
}
```

Private means that the field is hidden from other modules. To make fields public in the Nim language, you need to use `*` asterisk. On the other hand, if you want to make private fields you shouldn't use the asterisk.

These were an introduction to the Nim language. Let's see a couple of examples.

![](https://thepracticaldev.s3.amazonaws.com/i/dietndvjl68y6tu5hlrz.png)

## Installation

Nim works on Linux, Mac, and Windows. You can install using [this](https://nim-lang.org/install.html) link. I'm not using Windows, so I don't know how is the installation process should be.

For Linux and Mac, you can use **choosenim**.

[choosenim](https://github.com/dom96/choosenim#choosenim) is an installer for the Nim programming language. It allows you to easily switch between versions of Nim, whether that is the latest stable release or the latest development version.

To install the latest stable release of Nim using choosenim, just run the following in your terminal, then follow the onscreen instructions:

```bash
curl https://nim-lang.org/choosenim/init.sh -sSf | sh
```

I think this is the easiest way for the installation of the Nim.

**Installation with Package Managers**

Arch / Manjaro

```bash
pacman -S nim
```

Debian / Ubuntu

```bash
apt-get install nim
```

Docker

Get the latest stable image:

```bash
docker pull nimlang/nim
```

The latest development version:

```bash
docker pull nimlang/nim:devel
```

Fedora

```bash
dnf install nim
```

For the other distros visit [this](https://nim-lang.org/install_unix.html) link

## Hello World

In Nim language, we use echo for prints the given object to the standard output.

`echo` and `echo()` does the same thing.  You can call procedures without parentheses.

```nim
echo("Hello World")
```

I saved this code piece into the file called **hello_world.nim**

## Comments

Comments are important for programming languages. In the Nim langauge, there are different comment types.

**Single Line Comment**

I use hash for single line comment;

```nim
# This is a comment
```

**Multiline Comment**

Multiline comments are started with #[ and terminated with ]#. Multiline comments can also be nested.

```nim
#[
  This procedure gives Basket object.
  shopping(basket)
  #[
     Note: userID isn't accessible from the other modules.
  ]#
]#
```

You can also use the discard statement.

```nim
discard """ This procedure gives Basket object.
Note: userID isn't accessible from the other modules..
      shopping(basket) """
```

## Numbers

Numeric literals are written as in most other languages. But also you can use underscores in the numerical values. How?

```nim
var price = 1_000_000
```

And you can use **dot**, **e** or **E**

```nim
var price = 1.0e2

echo price # 100.0
```

## Var Statement

The var statement declares a new local or global variable. You can declare a single variable in a single line or multiline at the same time.

**Single Line**

```nim
var x, y: int
```

**Multiline**

```nim
var
    # user's name
    name: string

    # user's surname
    surname: string

    # user's phone
    phone: string
```

## Const and Let Statements

const and let are looks the same. Constants are symbols which are bound to a value. The constant's value cannot change. The compiler must be able to evaluate the expression in a constant declaration at compile time:

```nim
const x = "abc"
```

Indentation also can be used.

The let statement works like the var statement but the declared symbols are single assignment variables: After the initialization their value cannot change:

```nim
let x = "abc"
```

The difference between let and const is: let introduces a variable that can not be re-assigned, const means "enforce compile time evaluation and put it into a data section":

**const**

```nim
const input = readLine(stdin) # Error: constant expression expected
```

**let**

```nim
let input = readLine(stdin)   # works
```

## Compile and Run

To compile and run nim codes we use this command;

```bash
nim c -r hello_world.nim
```

It compiles the hello_world.nim file and run it.


## Conclusion

I really enjoyed with Nim language.

- You can use your nim codes to the C, CPP and JS languages.
- You can create type-safety variables.
- You will not say that "I didn't understand anything" if you're coming from the Python language.
- Object-Oriented Programming paradigm could be different than others.
- Procedures are methods in the Nim
- There is no self or this keyword in the Nim
- You should look at the standard library of Nim.

## Resources

I used these resources for this article:

- [Learn Nim by Official Nim Site](https://nim-lang.org/learn.html)
- [Nim Basics](https://narimiran.github.io/nim-basics/)
- [Nim by Example](https://nim-by-example.github.io/getting_started/)

Even if you will not use Nim, you should give a chance for a weekend activity to the Nim.

Thanks for reading. I hope you will enjoy this article.