Skip to content

Nim best practices

Timothee Cour edited this page Dec 6, 2020 · 13 revisions
  • no quit, use assert or doAssert
  • instead of str &= &"=\"{val}\"" use str &= &"={val.quoteShell}" (ie, quoteShell is more robust and doesn't add quote when un-needed)
  • use procs (possibly that run at CT) instead of macros whenever possible (as rule of thumb, whenever inputs are not untyped)
  • no global variables (or if really needed, only 1 that encapsulates all)
  • generated outputs all go somewhere under a single gitignored dir
  • see https://nim-lang.github.io/Nim/contributing.html#best-practices
  • see https://github.com/timotheecour/Nim/issues/415 nimlint

nimble packages

  • in source code for a package foo, use relative paths, eg import "."/bar instead of import foo/bar, so that it all works even in case of multiple versions of foo are installed
    • in tests, not sure TODO
  • sub-package eg: nimble install -y "http://github.com/genotrance/nimtreesitter?subdir=treesitter"
  • from PR in progress: nimble install https://github.com/timotheecour/dnsclient.nim@#pr_fix_3
  • foreignDep "libssl-dev"
template echoRun(cmd) =
  echo astToStr(cmd)
  cmd

task test, "test":
  # for lack of something better; this will be used in important_packages
  echoRun exec "nim doc src/std_vector.nim"

testing

  • testing a naked config:
XDG_CONFIG_HOME=$HOME/config_alt_XDG_CONFIG_HOME3/ NIMBLE_DIR=$HOME/.nimble_fake5 nimble test

backward compatibility

  • when (NimMajor, NimMinor, NimPatch) < (0, 19, 9):
Clone this wiki locally