Skip to content
Danil Yarantsev edited this page Apr 13, 2020 · 24 revisions

Nim Tips and tricks

A mix of bits of code, cookbook recipes, deep knowledge, tips and tricks about Nim programming.

Compilation

Syntax

Check syntax

Use nim check myfile.nim to check your program for errors, without code generation. This makes the process quicker.

Using reserved words as identifiers

You can use the slanted quote string syntax (stropping) if you need to use a reserved word as an identifier.

var `type`: int

Conditional compilation

Defining a compile time symbol

You want to define useSomeFeature at compilation time and use it in the code.

  • Do it when you compile: $ nim c -d:useSomeFeature myApp
  • Put it in the yourfilename.nims file located in the same directory as your source file yourfilename.nim as switch("define", "useSomeFeature")
  • Put it in the nim.cfg file located in the same directory as source file and write define:useSomeFeature
when defined(useSomeFeature):
  const someSubFeature = true
  type idType = int32
  var defaultUserName = "Frank"
else:
  type idType = int16
  var defaultUserName = "Molly"

# `when` does not open a new scope, so `idType` and `defaultUserName` are available here.

when isMainModule:
  # this code is ran only when this module is the main compilation module
  echo "This module was compiled on ", CompileDate, " at ", CompileTime

Errors

Error: attempting to call routine: 'a'

Nim likes spaces. This is a quirk of the command syntax where a +b is evaluated as a(+(b)). Use spaces, preferably, like a + b or else a+b, but don't mix space and no-spaces usage.

Error: expression '...' has no type (or is ambiguous)

  • Have you tried to discard the value from a proc that does not return any value?
proc noReturnValue =
  echo "Foo!"

discard noReturnValue()

Error: '...' doesn't have a concrete type, due to unspecified generic parameters.

  • A inner proc "inherits" the including proc generics declaration and you don't need to re-define them.
proc foo[T](x: T) =
  proc bar[T](y: T) =    <== Error is here
    echo "In bar"
  echo "In foo"

You must write:

proc foo[T](x: T) =
  proc bar(y: T) =    # Generic type T is defined by englobing proc
    echo "In bar"
  echo "In foo"

Tests

How to test that the compiler will reject a piece of code?

Use compiles.

import unittest

suite "valid syntax":
  test "syntax errors":
    let a = "foo"
    let b = 5
    assert not compiles(let c = a + b), "Can't mix string and int"

Enhance documentation with examples

You can provide examples of in your documentation, with runnableExamples. When the documentation is extracted from the source with $ nim doc myfile.nim, these examples are extracted and put in a temporary file, compiled and tested. This is a good way to provide examples for your API and check that they are still valid.

proc isOdd(i: int): bool =
  ## Test if its argument is odd.
  runnableExamples:
    assert 5.isOdd, "5 is an odd number"
    assert not isOdd(3 * 2), "6 is not an odd number"

  result = i mod 2 == 1

Macros

Dump macro expansion

dumpMacro shows how a macro call is expanded at compilation time. It's very useful for debugging macros...

import macros

let x, y = (4, 8)
expandMacros:
  echo "x > y is expanded to ", x > y

Output

Echo to stderr

echo writes to stdout. If you want to print to stderr use write and writeLine

writeLine(stderr, "Write to stderr")

How do I echo in a {.noSideEffect.} proc?

You can use debugEcho for this.

Enums

Converting string to enum

Strings can be converted to enum using parseEnum from strutils. Beware that a ValueError is raised if the enum is not found. If you don't want to manage the exception, provide a default with parseEnum.

import strutils

type
  Fruit = enum
    Apple,
    Banana,
    Cherry

  let fruit = parseEnum[Fruit]("cherry")
  let pineapple = parseEnum[Fruit]("pineapple", Banana)

Language details

Shallow vs. DeepCopy

Clone this wiki locally