DEV Community

Cover image for Getting Started Nim Lang
Itachi Uchiha
Itachi Uchiha

Posted on

Getting Started Nim Lang

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;

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

  saveShopping(basket)
Enter fullscreen mode Exit fullscreen mode

You could use this method like that;

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)
Enter fullscreen mode Exit fullscreen mode

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

public class Basket
{
  private int userID { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

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.

Installation

Nim works on Linux, Mac, and Windows. You can install using this 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 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:

curl https://nim-lang.org/choosenim/init.sh -sSf | sh
Enter fullscreen mode Exit fullscreen mode

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

Installation with Package Managers

Arch / Manjaro

pacman -S nim
Enter fullscreen mode Exit fullscreen mode

Debian / Ubuntu

apt-get install nim
Enter fullscreen mode Exit fullscreen mode

Docker

Get the latest stable image:

docker pull nimlang/nim
Enter fullscreen mode Exit fullscreen mode

The latest development version:

docker pull nimlang/nim:devel
Enter fullscreen mode Exit fullscreen mode

Fedora

dnf install nim
Enter fullscreen mode Exit fullscreen mode

For the other distros visit this 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.

echo("Hello World")
Enter fullscreen mode Exit fullscreen mode

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;

# This is a comment
Enter fullscreen mode Exit fullscreen mode

Multiline Comment

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

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

You can also use the discard statement.

discard """ This procedure gives Basket object.
Note: userID isn't accessible from the other modules..
      shopping(basket) """
Enter fullscreen mode Exit fullscreen mode

Numbers

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

var price = 1_000_000
Enter fullscreen mode Exit fullscreen mode

And you can use dot, e or E

var price = 1.0e2

echo price # 100.0
Enter fullscreen mode Exit fullscreen mode

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

var x, y: int
Enter fullscreen mode Exit fullscreen mode

Multiline

var
    # user's name
    name: string

    # user's surname
    surname: string

    # user's phone
    phone: string
Enter fullscreen mode Exit fullscreen mode

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:

const x = "abc"
Enter fullscreen mode Exit fullscreen mode

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:

let x = "abc"
Enter fullscreen mode Exit fullscreen mode

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

const input = readLine(stdin) # Error: constant expression expected
Enter fullscreen mode Exit fullscreen mode

let

let input = readLine(stdin)   # works
Enter fullscreen mode Exit fullscreen mode

Compile and Run

To compile and run nim codes we use this command;

nim c -r hello_world.nim
Enter fullscreen mode Exit fullscreen mode

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:

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.

Latest comments (5)

Collapse
 
adityapadwal profile image
Aditya Padwal

Awesome Ali, would be great to have you in below group.

linkedin.com/groups/10546099

Collapse
 
navicsteinr profile image
Navicstein Rotciv

Nice article, NIM is one of the under looked languages in our tech world simply because its not backed by a huge company, i wish huge companies like microsoft or google will start using it

Collapse
 
dvska profile image
Dmitry / skype: dvska-at-skype

Hey,
Wake up!
Nim 1.0 already released!!!

Collapse
 
dmknght profile image
dmknght

Nim has perfect cross-compiling feature. For example: I just need to add flags -d:mingw --cpu:amd64 on my Linux machine to compile a x64 windows binary file.

Collapse
 
nepeckman profile image
nepeckman

Nim is one of my favorite languages. Been using it for side projects for the last year and haven't found anything with the same power and utility. Macros are awesome, compiled binaries are useful, and its just so easy to get started with it. Looking forward to that eventual 1.0 release!