Method Call Syntax
In Nim, a.f(b) is really just syntactic sugar for f(a, b). Note: they look like methods but aren’t attached to the type/object at all. Which function to call will be figured out at compile time, just like normal function calls.
You can use this to implement pseudo extension methods on standard types like strings:
import
nim_utils/files,
os,
strformat
proc makeBackup(filename: string) =
let backupPath = fmt"{filename}.bak"
case filename.fileType
of ftFile, ftSymlink:
copyFile(filename, backupPath)
of ftDir:
copyDir(filename, backupPath)
else:
raise newException(IOError, fmt"Cannot make backup of {filename}")
for f in @["fstab", "systemd"]:
fmt"/etc/{f}".makeBackup() # Same as makeBackup(fmt"/etc/{f}")
You can also use this to chain calls together nicely:
[Read More]