An introduction

What is Nim?

Nim is an incredibly fast systems programming language that is expressive, extensible and really fun to use.



              echo("hello, world")
            

Who develops Nim?

Volunteers

2,000+ PRs

2,500+ issues solved

500+ packages

$1500+ monthly contributions

Why Nim?

Let me whet your appetite...

...with a demo.

Snake

https://picheta.me/snake/

Vektor 2089

Search "Vektor 2089"

Goals

  • Efficiency of a statically-typed compiled language with the ease of programming of a dynamic language, done right.
  • Reduce typing; no more:
    
                    foo.Foo foo = new foo.Foo(foo.INIT_FOO);
                  
  • Fast!
  • Reduce boilerplate
  • Dependency-free programs
  • Cross-platform programs
  • Type and memory safe

Implementation

  • Official compiler is written in Nim
  • Compiles Nim source code to C/C++/Objective C and JavaScript
  • Blazingly fast compilation speed
  • No need for makefiles

Compilation speed

Time for another demo!

Basics


            const N = 1024 # int
            const str = "a simple юникода string\n"

            var x, y: float
            let ch = '\u1234'

            #[ Define and use a type ]#
            type
              T = ref object
                a, b: int
            var t0 = T(a: 42, b: 0)

            # Control structures
            if len(str) > 0:
              x = N # no need to explicitly cast
          

Values and types


            let names = ["Liam", "Andy"]

            var numbers = @[1, 2, 3]
            numbers.add(4)

            let phoneBook = {
              "John": "07712345678", "Jeremy": "0281234567", # ...
            }.toTable()

            type
              Day = enum
                Monday, Tuesday, Wednesday, Thursday, Friday,
                Saturday, Sunday

            let weekend = {Saturday, Sunday}
          

Procedures


            import math
            type
              Point* = object
                x*, y*: float

            proc scale*(p: var Point, s: float) =
              p.x *= s
              p.y *= s

            proc abs*(p: Point): float =
              return sqrt(p.x*p.x + p.y*p.y)

            var x = Point(x: 3, y: 4)
            x.scale(5) # equivalent to scale(x, 5)
            assert x.x == 15
          

Procedure on any type


            proc say*(s: string) =
              echo("Computer says: ", s)

            "No".say()
            say("No")
          

Procedure overloading


            proc say*(s: string) =
              echo("Computer says: ", s)

            proc say*(s: string, count: int) =
              for i in 0 .. <count:
                say(s)

            "No".say(5) # Output (x5): Computer says: No
          

Generics


            proc say*[T](s: T) =
              echo("Computer says: ", s)

            say(0b01101011) # Output: Computer says: 107
          

Metaprogramming

Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse and/or transform other programs...
  • This is Nim's specialty
  • Reduces boilerplate significantly
  • Generics, templates and macros

Domain Specific Languages


            import jester, asyncdispatch, htmlgen

            routes:
              get "/":
                resp h1("Hello world")

            runForever()
          

Compile-time execution


            proc fibonacci(n: int): int =
              if n <= 2:
                result = 1
              else:
                result = fibonacci(n - 1) + fibonacci(n - 2)

            const fib20 = fibonacci(20)
            echo fib20 # output: 6765
          

Other goodies

  • Iterators
  • Concurrency via async await
  • Exceptions
  • Effect tracking
  • Soft real-time garbage collector
  • Official package manager
  • Concepts/Interfaces
  • Rich standard library

My book

nim-lang.org/manning

40% off: ctwnidevconf

Other links

Thank you for listening!