Nim compiles fine to executables.
It integrates well with QT6 & C++.
Here some demo code,
Tip, to configure zed editor with language server, coloring & completetion.
~/.config/zed/settings.json
Tip , nim.cfg for testing,
It integrates well with QT6 & C++.
Here some demo code,
Code:
# Tuple on Stack
type
PersonStack = tuple
name: string
age: int
var
ap: PersonStack = (name: "Peter", age: 30)
echo ap.name, ":",ap.age
proc printit(p: PersonStack) =
echo p.name
echo p.age
ap.printit()
#-----------------------------------------------------------
#Class on Stack
type StudentStack = object of RootObj
naam: string
score: int
var
s1:StudentStack = StudentStack(naam: "Piet", score: 85)
#------------------------------------------------------------
#Class on Heap
type StudentHeap = ref object of RootObj
naam: string
score: int
var
s2:StudentHeap = StudentHeap(naam: "Piet", score: 85)
#------------------------------------------------------------
# Sum types
type
ValueKind = enum
vkInt, vkString
FlexibleValue = object
case kind: ValueKind
of vkInt:
intVal: int
of vkString:
strVal: string
# 1. Create as an integer
var data1 = FlexibleValue(kind: vkInt, intVal: 100)
# 2. Create as a string
var data2 = FlexibleValue(kind: vkString, strVal: "Nim is powerful")
# 3. Accessing the data safely
proc display(val: FlexibleValue) =
case val.kind
of vkInt:
echo "Integer value: ", val.intVal
of vkString:
echo "String value: ", val.strVal
display(data1)
display(data2)
#------------------------------------------------------------
#Dynamic dispatch
type
Animal = ref object of RootObj
Dog = ref object of Animal
Cat = ref object of Animal
# Use 'method' for dynamic dispatch
method makeNoise(a: Animal) {.base.} =
echo "Generic animal sound"
method makeNoise(d: Dog) =
echo "Woof!"
method makeNoise(c: Cat) =
echo "Meow!"
# This list holds different types (Animal, Dog, Cat)
let zoo: seq[Animal] = @[Animal(), Dog(), Cat()]
for a in zoo:
a.makeNoise() # Dispatched at runtime
#------------------------------------------------------------
Tip, to configure zed editor with language server, coloring & completetion.
~/.config/zed/settings.json
Code:
{
"languages": {
"Nim": {
"format_on_save": "off",
"language_servers": ["nimlangserver"]
}
},
"lsp": {
"nim": {
"binary": {
"path": "/usr/local/bin/nimlangserver"
},
"initialization_options": {
"nimsuggestPath": "/usr/local/bin/nimsuggest",
"nimSuggestPath": "/usr/local/bin/nimsuggest"
}
}
}
}
Tip , nim.cfg for testing,
Code:
--warnings:on
--hints:on
--styleCheck:error
--checks:on
--boundChecks:on
--overflowChecks:on
--nilChecks:on
--stackTrace:on
--lineDir:on
--mm:orc
--threads:on
--backend:cpp
--passC:"-Wno-array-bounds"
--opt:none