Tuple to function arguments in Nim
Asked Answered
A

1

9

Can I convert a tuple into a list of function arguments in Nim? In other languages this is known as "splat" or "apply".

For example:

proc foo(x: int, y: int) = echo("Yes you can!")

type:
  Point = tuple[x, y: int]

let p: Point = (1,1)

# How to call foo with arguments list p?
Ambrosio answered 24/1, 2018 at 8:57 Comment(0)
M
9

I haven't seen this in the stdlib or any other lib, but you can certainly do it yourself with a macro:

import macros

macro apply(f, t: typed): typed =
  var args = newSeq[NimNode]()
  let ty = getTypeImpl(t)
  assert(ty.typeKind == ntyTuple)
  for child in ty:
    expectKind(child, nnkIdentDefs)
    args.add(newDotExpr(t, child[0]))
  result = newCall(f, args)

proc foo(x: int, y: int) = echo("Yes you can!")

type Point = tuple[x, y: int]

let p: Point = (1,1)

# How to call foo with arguments list p?
apply(foo, p) # or:
foo.apply(p)

Further testing would be required to make sure this works with nested tuples, objects etc. You also might want to store the parameter in a temporary variable to prevent side effects from calling it multiple times to get each tuple member.

Mukluk answered 24/1, 2018 at 9:25 Comment(3)
Very nice! Lots of stuff I don't understand in there, though. Looks like I'll have to get up to speed on macros.Ambrosio
@Imran: Apart from reading the docs, one of the easiest ways to getting started with macros is to place some echos on things like result.repr, result.treeRepr at the end of the macro and the same for the macro arguments f and t.Morelli
It would be better if there were some concept restriction on the accepted types, like "is tuple". and there must be a way to replicate python's asterisk tooTanah

© 2022 - 2024 — McMap. All rights reserved.