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)
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)
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; }
}
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
I think this is the easiest way for the installation of the Nim.
Installation with Package Managers
Arch / Manjaro
pacman -S nim
Debian / Ubuntu
apt-get install nim
Docker
Get the latest stable image:
docker pull nimlang/nim
The latest development version:
docker pull nimlang/nim:devel
Fedora
dnf install nim
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")
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
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.
]#
]#
You can also use the discard statement.
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?
var price = 1_000_000
And you can use dot, e or E
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
var x, y: int
Multiline
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:
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:
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
const input = readLine(stdin) # Error: constant expression expected
let
let input = readLine(stdin) # works
Compile and Run
To compile and run nim codes we use this command;
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:
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.
Top comments (5)
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!
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.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
Hey,
Wake up!
Nim 1.0 already released!!!
Awesome Ali, would be great to have you in below group.
linkedin.com/groups/10546099