You should give Nim a try

Medium says I need a photo; otherwise you won’t read this article. So here’s one by Jasper Boer on Unsplash

Syntax, Syntax, Syntax

Some portion of Earth’s population of humans who dedicate time out of their day to author code meant to be executed by computers express the preference of verbosity as a means of producing clarity and reducing ambiguity in said code.

var
longest = 0
count = 0
for line in stdin.lines:
count += 1
if line.len > longest:
longest = line.len
echo "Lines: ", count
echo "Longest: ", longest
  • Pythonic indentation instead of curly braces. I’m not opposed to curly braces — they’re fine, too.
  • Optional parentheses. In the snippet above, stdin.lines and line.len are both functions. Also, echo is being using without parens.
  • Uniform Function Call Syntax. It let’s you interchangeably use len(line) or line.len() — read more about it here.
  • Macros and Templates. These let you make code that makes code. I’ll give an example below.
import argparsevar p = newParser("My Program"):
flag("-n", "--dryrun", help="Don't actually do stuff")
command("add"):
flag("-a", "--all", help="Add all files")
arg("dir")
run:
echo "All: ", opts.all
echo "Running on dir: ", opts.dir
command("prefix"):
option("-p", "--prefix", default="backup")
run:
echo "Running with prefix: ", opts.prefix
p.run()

Nake

Nake is a Nim library that lets you write Make-like files in Nim. It doesn’t do the same dependency-graph stuff as Make, but if you need a lot of that, you can build it in. Here’s an example Nakefile which you could execute with nake debug-build:

import nakeconst
ExeName = "my_app"
BleedingEdgeFeatures = "-d:testFeature1 -d:testFeature2"
task "debug-build", "Build with unproven features":
if shell(nimExe, "c", BleedingEdgeFeatures, "-d:debug", ExeName):
## zip up files or do something useful here?
when defined(windows):
echo "Do windows specific things"
elif defined(linux):
echo "Do linux specific things"
...

Cross-Compiling

Nim makes it fairly easy to cross-compile (once you find the right documentation). I use macOS for most of my work, but I need to have a Windows VM handy to build some projects. I want the VM to be predictably built, so I’ve created a Nim program that compiles to a single (massive) Windows executable. It’s massive because it includes all the MSI and Zip packages I need to install on my Windows box. Here’s a snippet showing how slurp gathers the installers into the executable:

...const mingwZip = slurp("tmp/mingw64.zip")
const gitExe = slurp("tmp/Git32.exe")
const dllZip = slurp("tmp/dlls.zip")
const nodemsi = slurp("tmp/node.msi")
const yarnmsi = slurp("tmp/yarn.msi")
...proc ensure_node() =
try:
if run(@["node", "--version"]) != 0:
raise newException(CatchableError, "Node not installed")
except:
echo "Installing node..."
createDir("C:"/"tmp")
withDir("C:"/"tmp"):
writeFile("node.msi", nodemsi)
discard run(@["msiexec", "/qn", "/passive", "/i", "C:"/"tmp"/"node.msi"])
removeFile("node.msi")
echo "Installed node!"
...

Speed

The Nim compiler generates C/C++/Obj-C and then compiles that. It generates very fast code.

C-Interoperability

Interoperating with C is very easy. You can use the {.compile.} pragma to pull C files into your code or use some of the import pragmas to link dynamically or statically to C/C++ libraries. Read more here.

Caveats

  • Some people complain about Nim’s unique “case sensitivity” method. The following identifiers are all equivalent: someArg, sOmearG , some_arg , some_Arg. In theory, I don’t like this and I hope it eventually goes away. In practice it hasn’t ever been a problem. I didn’t even know this was a thing even after almost a year of use.
  • Backward-compatibility. Nim is still in active development and there are often breaking changes between releases. This isn’t a complaint — you just have to be aware of it.
  • There’s lots of features. Perhaps too many. I imagine many features will get trimmed out before v1.0 is reached.
  • I’ve only written solo programs with Nim. I don’t know what it would be like to use Nim in a collaborative environment.

Go play!

In conclusion, go install Nim or try it online. Take it for a spin. Complain a little. Learn a little. Smile a little. It won’t hurt and you might be surprised how pleasant it is!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store