nim-wiki/Tips-and-tricks.md

1.1 KiB

  • 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

Tricks with conditional compilation

when defined(useSomeFeature):
  const someSubFeature = true
  type ID_Type = int32
  var defaultUserName = "Frank"
else:
  type ID_Type = int16
  var defaultUserName = "Molly"

# `when` does not open a new scope, so `ID_Type` 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

Defining useSomeFeature

  • Do it when you compile: $ nim c -d:useSomeFeature myApp
  • Put it in the local nim.cfg file define:useSomeFeature

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

You can use debugEcho for this.

Language details

Shallow vs. DeepCopy