Skip to content

Instantly share code, notes, and snippets.

@Yardanico
Last active October 18, 2017 12:56
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 Yardanico/3ed3f8d8b6ccd070afd67e8bd26f2914 to your computer and use it in GitHub Desktop.
Save Yardanico/3ed3f8d8b6ccd070afd67e8bd26f2914 to your computer and use it in GitHub Desktop.
import macros
macro unpack(x: typed, len: static[int]): untyped =
result = newTree(nnkPar)
for ind in 0..<len:
result.add quote do:
`x`[`ind`]
let a = @[1, 2, 3, 4]
let (var1, var2, var3) = a.unpack(3)
echo var1, var2, var3
let c = "hello"
let (ca, cb, cc) = c.unpack(3)
echo ca, cb, cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment