Skip to content

Instantly share code, notes, and snippets.

@gradha
Created December 26, 2013 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gradha/8140318 to your computer and use it in GitHub Desktop.
Save gradha/8140318 to your computer and use it in GitHub Desktop.
Nimrod macro to unpack values from an array
import macros
macro unpackArray(arr, count: expr): expr {.immediate.} =
var source = "("
for i in 0..count.intVal-1:
if i > 0: source.add(',')
source.add(arr.toStrLit.strVal & "[" & $i & "]")
source.add(')')
result = parseStmt(source)
type TIntArray = array[0..5, int] # an array that is indexed with 0..5
var x: TIntArray
x = [1, 2, 3, 4, 5, 6]
let (d1, d2, d3) = x.unpackArray(3)
echo d1, d2, d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment