Reputation: 15336
Is it possible to unpack tuple in arguments?
import sugar, sequtils
echo @[(1, 1), (2, 2)].filter((point) => point[0] > 1) # Works
echo @[(1, 1), (2, 2)].filter(((x, y)) => x > 1) # Error
Upvotes: 2
Views: 551
Reputation: 1473
The sugar macro =>
can sort of unpack tuple arguments, as the following example (taken from docs) shows:
proc passTwoAndTwo(f: (int, int) -> bool): bool =
f(2, 2)
echo passTwoAndTwo((x, y) => x > 1) # true
Sort of, since it is actually treating x and y as two different arguments, not as element of a tuple.
In fact, the problem is the syntax ((x, y)) => x > 1
which is not supported by =>
(you can check that by running it with a discard
in front).
I am not sure how easy or reasonable would be to require =>
to support the special case of passing a tuple.
For your use cases the following two lines are working alternatives once you have a Point
type defined:
echo @[(1, 1).Point, (2, 2)].filter(point => point.x > 1)
echo @[(1, 1), (2, 2)].filterIt(it.Point.x > 1)
Upvotes: 3