ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward
Learn how to get started with Nim
Find out why Nim is interesting
See Nim syntax and semantics
Code: https://github.com/tedneward/Demo-Nim
Slides: http://www.newardassociates.com/presentations/BusyDevsGuide/Nim.html
Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula.
https://nim-lang.org/
statically-typed
compiled to native ("native dependency-free") or JavaScript
memory management is deterministic and customizable
compile-time evaluation of some constructs
support for different backends (C, C++, JavaScript)
macro system offering full AST manipulation (at compile-time)
Download: https://nim-lang.org/install.html
Homebrew/Linuxbrew: brew install nim
Hello world
# This is a comment echo "What's your name? " var name: string = readLine(stdin) echo "Hi, ", name, "!"
Compiling, then executing
$ nim c hello.nim $ ./hello What's your name? Ted Hi, Ted!
Compiling and executing in one run
$ nim compile -r hello.nim
Single-line comments start with #
Documentation comments start with ##
Multi-line comments bracketed with #[ and ]#
and can be nested
Syntax
# This is a comment
#[ This is
a multiline
comment ]#
declaration name : type = value
Declarations
var: mutable/variable
const: compile-time-known immutable binding
let: non-compile-known immutable binding
names are UTF-8 strings
case- and underscore-insensitive, except for first character
type is optional if value is provided (type-inference)
Multiple declaration syntax is supported
Declarations in Nim
var a: int # mutable integer, no value explicitly set
var b = 7 # mutable inferred integer set to 7
var
c = -11 # mutable inferred integer set to -11
d = "Hello" # mutable inferred string set to "Hello"
e = '/' # mutable inferred character set to '/'
var f1, f2 = 37 # mutable inferred integer set to variables
const g = 35 # immutable integer set to compile-time value
var k = 27
let j = 2 * k # immutable integer set to runtime value
true, false literals
Relational operators:
==: equality
!=: inequality
less-than, greater-than per usual
less-than-equal, greater-than-equal per usual
Logical operators:
and: both operands must be true
or: either operand can be true
xor: true if only one operand is true
not: negates the truth value of the operand
Booleans in Nim
let
rg = 31
rh = 99
echo "rg is greater than rh: ", rg > rh
echo "rg is smaller than rh: ", rg < rh
echo "rg is equal to rh: ", rg == rh
echo "rg is not equal to rh: ", rg != rh
echo "T and T: ", true and true
echo "T or F: ", true or false
echo "F xor F: ", false xor false
echo "not F: ", not false
int
literals use _ as thousands separator
usual mathematical operators: +, -, *, /
+, -, * produce integer result
/ produces floating-point result
div produces integer (no-remainder) result
mod produces modulo (remainer-only) result
float
follows floating-point standard
literals support scientific (exponentiation) notation: 4e7
usual mathematical operators: +, -, *, /
no div or mod
int function converts parameter to int type
float function convers parameter to float type
Nim numerics
let na = 11 var nb = (na + 5 - (3 * 2)) / 3 var nc = int(5.5) var nd = float(5)
single ASCII character
multiple-character literals produce error
literals are offset by ticks (')
special characters:
\n: newline character
\t: tab character
\: backslash character
series of characters
literals are enclosed in double quotes (")
concatenation via &
special characters also supported
"raw" r-prefixed strings ignore special characters
"long strings" are """-enclosed strings
strings are objects
Nim strings and characters
var sa = "Hello"
let sb = sa & "\nworld\n"
let sr = r"Hello\tworld" # raw string: "Hello world"
sa.add(" the world")
echo "The sa string equals", sa
let
ci = 'a'
cj = 'd'
echo ci < cj
let
sm = "axyb"
sn = "axyz"
so = "ba"
sp = "ba "
echo sm < sn
echo sn < so
echo so < sp
Arrays
Sequences
Tuples
"homogeneous container of constant length"
size must be known at compile-time
array[length,type] = [values]
values are comma-separated list of values
length and type can be inferred from values
Arrays
var
a: array[3, int] = [5, 7, 9]
b = [5, 7, 9]
c = [] # error
d: array[7, string]
const m = 3
let n = 5
var ba: array[m, char]
"homoegeneous container of flexible length"
size doesn't have to be known at compile-time
size can change at runtime
seq[type] = @[values]
if values are present, sequence declaration can be inferred
or newSeq[type]() procedure call
Sequences
var
e1: seq[int] = @[]
f = @["abc", "def"]
e = newSeq[int]()
g = @['x', 'y']
h = @['1', '2', '3']
g.add('z') # x, y, z
h.add(g) # 1, 2, 3, x, y, z
var i = @[9, 8, 7] # 3
i.add(6) # 4
both arrays and sequences use [-] indexing syntax
positive index starts 0-indexed from the left
^-prefixed index starts 1-indexed from the right
start .. end (range) is a "slice", inclusive of both ends
start ..< end is a "slice", exclusive of end
Indexing and slicing
let j = ['a', 'b', 'c', 'd', 'e']
echo j[1] # a
echo j[^1] # e
echo j[0 .. 3] # @[a, b, c, d]
echo j[0 ..< 3] # @[a, b, c]
heterogenous mutable-contents fixed-size containers
( values ) where values is a comma-separated list of values
( name : value ) named-field tuple
access fields by name or position
[ index ]
.name
Tuples
let n = ("Banana", 2, 'c')
echo n # (Field0: "Banana", Field1: 2, Field2: 'c')
var o = (name: "Banana", weight: 2, rating: 'c')
o[1] = 7
o.name = "Apple"
echo o # (name: "Apple", weight: 7, rating: 'c')
if condition :
elif condition : (optional)
else:
indentation-based scope blocks
If
var x = 10
if x < 5:
echo "x is less than 5"
elif x > 5:
echo "x is greater than 5"
else:
echo "x is equal to 5"
case expression
of value :
else: (optional)
each of block has indentation scope
Case
let i = 7
case i
of 0:
echo "i is zero"
of 1, 3, 5, 7, 9:
echo "i is odd"
of 2, 4, 6, 8:
echo "i is even"
else:
echo "i is too large"
While
var a = 1
while a*a < 100:
echo "a is: ", a
inc a
echo "final value of a: ", a
For
for n in 5 ..< 9:
echo n
for n in countup(0, 16, 4):
echo n
break: Early loop termination
continue: Early loop re-cycle
Nim is a good language to know
Architect, Engineering Manager/Leader, "force multiplier"
http://www.newardassociates.com
http://blogs.newardassociates.com
Books
Developer Relations Activity Patterns (w/Woodruff, et al; APress, 2026)
Professional F# 2.0 (w/Erickson, et al; Wrox, 2010)
Effective Enterprise Java (Addison-Wesley, 2004)
SSCLI Essentials (w/Stutz, et al; OReilly, 2003)
Server-Based Java Programming (Manning, 2000)