kcvinu
05-10-2018, 20:15
Hi all,
I would like to share some interesting stuff about the Nim programming language.
Nim compiles code to first C/C++/Obj-C/JS code and then use a C/C++/Obj-C/JS compiler to make executable.
Nim's syntax is much like python. But you can taste some pascal style in Nim.
Nim has a minimal support for object oriented programming. It uses Types for it. Here is the syntax
type myClass = ref object of RootObj # an object lives in stack, a ref object lives in heap.
aMember : int
anotherMember : string
The main work horse in Nim is "proc", a Nim version for function and sub. Another best feature i love in Nim is Uniform Function Call Syntax aka UFCS. Assume that you have a function like this.
proc aFunction(param1 : string, param2 : int, param3 : bool) : bool
Then you can call that function like this -
param1.aFunction(param2, param3)
Another plus points
1. Nim uses Garbage Collector
2. You can inherit a Nim class with "of" keyword.
3. You can use pragmas. for ex: "{. pure.}" is a pragma. If you use it with an enum, then you must use name of that enum whenever you use any element of that enum.
4. Nim uses a magical "result" variable in functions. "result" is automatically the function result. So you dont need to create and assign one.
5. Classes created with "ref" keyword are initializing with "new" keyword.
6. To declare a variable or a function as public, you just need to put an "*" sign after the identifier.
Here is a sample code to understand Nim style.
type
aClass = ref object of RootObj # A base class
intVar : int
strVar : string
aChildClass = ref object of aClass # A child class which inherits from aClass
aVar : int
boolVar : bool
proc NewClass(iVar : int) : aClass = # Constructor of aClass.
result = new aClass
result.intVar = iVar
result.strVar = "Hi I am aClass member"
proc NewChildClass() : aChildClass =
result = new aChildClass
result.intVar = 500 # Accessing base class member
echo "Hi I am from child class" # Printing some text
proc childMethod(me : aChildClass) : int =
result = me.intVar # Magical result variable.
#Usage of this classes.
var aBase = NewClass(62) # var is the dim in Nim
var aChild = NewChildClass()
echo aChild.childMethod() # prints 500. See the UFCS is interesting...
I would like to share some interesting stuff about the Nim programming language.
Nim compiles code to first C/C++/Obj-C/JS code and then use a C/C++/Obj-C/JS compiler to make executable.
Nim's syntax is much like python. But you can taste some pascal style in Nim.
Nim has a minimal support for object oriented programming. It uses Types for it. Here is the syntax
type myClass = ref object of RootObj # an object lives in stack, a ref object lives in heap.
aMember : int
anotherMember : string
The main work horse in Nim is "proc", a Nim version for function and sub. Another best feature i love in Nim is Uniform Function Call Syntax aka UFCS. Assume that you have a function like this.
proc aFunction(param1 : string, param2 : int, param3 : bool) : bool
Then you can call that function like this -
param1.aFunction(param2, param3)
Another plus points
1. Nim uses Garbage Collector
2. You can inherit a Nim class with "of" keyword.
3. You can use pragmas. for ex: "{. pure.}" is a pragma. If you use it with an enum, then you must use name of that enum whenever you use any element of that enum.
4. Nim uses a magical "result" variable in functions. "result" is automatically the function result. So you dont need to create and assign one.
5. Classes created with "ref" keyword are initializing with "new" keyword.
6. To declare a variable or a function as public, you just need to put an "*" sign after the identifier.
Here is a sample code to understand Nim style.
type
aClass = ref object of RootObj # A base class
intVar : int
strVar : string
aChildClass = ref object of aClass # A child class which inherits from aClass
aVar : int
boolVar : bool
proc NewClass(iVar : int) : aClass = # Constructor of aClass.
result = new aClass
result.intVar = iVar
result.strVar = "Hi I am aClass member"
proc NewChildClass() : aChildClass =
result = new aChildClass
result.intVar = 500 # Accessing base class member
echo "Hi I am from child class" # Printing some text
proc childMethod(me : aChildClass) : int =
result = me.intVar # Magical result variable.
#Usage of this classes.
var aBase = NewClass(62) # var is the dim in Nim
var aChild = NewChildClass()
echo aChild.childMethod() # prints 500. See the UFCS is interesting...