I want to achieve something like this:
# this works perfectly fine:
func hello(T: typedesc): int =
sizeof T
echo hello(float64)
echo hello(float32)
# this doesn't work:
func funfunfun(Ts: openArray[typedesc]): int =
for T in Ts:
result += sizeof T
echo funfunfun([float64, float32])
I found a GitHub issue that suggest that it isn't wanted that this specific example works, however, I didn't manage to find an alternative solution. Generally, I would like to have similar functionality to the parameter pack template feature of C++.
Before I start to think about how this can be done with a macro, I wanted to ask, if there is maybe simpler solution that I missed.
If you really don't want to use a macro you could create a dummy variable of a tuple of the types you want like this:
proc niceSize(a: tuple): int =
for f in a.fields:
result += sizeof f
var temp: (int64, int8, int16)
echo niceSize(temp)
Note: sizeof temp != niceSize(temp) as the tuple's memory can contain "holes" for memory efficiency. In the case of (int, int8) sizeof temp == 16 while niceSize(temp) == 9