Busy Developer's Guide to Nim

ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward

Objectives

What do we want to do today?

Objectives

Notes before we begin:

Nim Overview

What is this thing?

Nim Overview

Official description

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/

Nim Overview

Official description

Getting Started

From 0 to Hello World

Getting Started

Installation

Getting Started

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

Basics

Some core elements

Basics

Lexical

Basics

Syntax

# This is a comment
    
#[ This is
    a multiline
    comment ]#

Basics

Local bindings

Basics

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

Basics

Primitive types: Boolean

Basics

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

Basics

Primitive types: Integers

Basics

Primitive types: Floats

Basics

Integer/float conversions

Basics

Nim numerics

let na = 11
var nb = (na + 5 - (3 * 2)) / 3
var nc = int(5.5)
var nd = float(5)

Basics

Primitive types: Characters

Basics

Primitive types: Strings

Basics

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  

Composite Types

Molecules out of atoms

Composite Types

These are the types that are made up of other elements

Composite Types

Arrays

Composite Types

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]

Composite Types

Sequences

Composite Types

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

Composite Types

Indexing and Slicing

Composite Types

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]

Composite Types

Tuples

Composite Types

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')

Control flow

Ifs and elses and loops, oh my!

Control flow

Branching: if

Control flow

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"

Control flow

Branching: case

Control flow

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"

Control flow

Looping: while

Control flow

While

var a = 1
    
while a*a < 100: 
  echo "a is: ", a
  inc a         
    
echo "final value of a: ", a

Control flow

Looping: for

Control flow

For

for n in 5 ..< 9: 
  echo n
    
for n in countup(0, 16, 4):  
  echo n

Control flow

Loop manipulation

Procedures

Named reusable blocks of code

Modules

Atomic units of deployment

Foreign Function Interface (FFI)

Calling C from Nim

Macros and metaprogramming

Going within the compiler

Nim Resources

Where to go to go get more

Summary

So... now what?

Credentials

Who is this guy?