-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Tips and tricks
A mix of bits of code, cookbook recipes, deep knowledge, tips and tricks about Nim programming.
Use nim check myfile.nim
to check your program for errors, without code generation. This makes the process quicker.
You can use the slanted quote string syntax (stropping) if you need to use a reserved word as an identifier.
var `type`: int
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 fileyourfilename.nim
asswitch("define", "useSomeFeature")
- Put it in the
nim.cfg
file located in the same directory as source file and writedefine: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
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.
- Have you tried to
discard
the value from a proc that does not return any value?
proc noReturnValue =
echo "Foo!"
discard noReturnValue()
- 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"
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"
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
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
echo
writes to stdout
. If you want to print to stderr
use write and writeLine
writeLine(stderr, "Write to stderr")
You can use debugEcho for this.
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)
Intro
Getting Started
- Install
- Docs
- Curated Packages
- Editor Support
- Unofficial FAQ
- Nim for C programmers
- Nim for Python programmers
- Nim for TypeScript programmers
- Nim for D programmers
- Nim for Java programmers
- Nim for Haskell programmers
Developing
- Build
- Contribute
- Creating a release
- Compiler module reference
- Consts defined by the compiler
- Debugging the compiler
- GitHub Actions/Travis CI/Circle CI/Appveyor
- GitLab CI setup
- Standard library and the JavaScript backend
Misc