Created
December 26, 2013 23:49
-
-
Save gradha/8140318 to your computer and use it in GitHub Desktop.
Nimrod macro to unpack values from an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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