Sometimes one does not need all values of a tuple, is it possible to not declare an identifier as in the example below:
proc f(): tuple[s: string, i: int] =
("x", 1)
let (?, i) = f() # I don't want to set an identifier here
The same counts for the type of the tuple, can one leave out the names of the tuple and write tuple[string, int] instead?
You don't need to assign all values of a tuple:
let i = f().s
or use the subscript operator:
let i = f()[0]
I don't think you can avoid giving names to a tuple, just use single letter names. If the contents are all of the same type you can use an array instead of a tuple.I wouldn't worry that much over unused variables and just give it a name you are not going to use, like dummy or ignored. If you are worried about the nimrod compiler spitting unused warnings, you could request a feature to avoid outputting warnings for a specific set of variable names, like dummy, ignored, temp, tmp, etc. Most likely release builds and good C compilers will make the variable a nop.
You can also use a temporary variable and create tuples on the fly:
import os
let
split_result = path.splitFile()
(d, e) = (split_result.dir, split_result.ext)
echo "dir ", d
echo "e ", e
The temporary assignment is to avoid calling splitFile multiple times.Forgot to mention. If you know the amount of parameters you want to unpack, you can write a quick template to do it:
import os
const
path = "/some/absolute/path.txt"
template unpack(stuff, p1, p2: expr): expr {.immediate.} =
let f = stuff
var result {.gensym.}: tuple[p1: type(f.p1), p2: type(f.p2)]
result.p1 = f.p1
result.p2 = f.p2
result
let
(a, b) = unpack(path.splitFile(), ext, name)
echo "a ", a
echo "b ", b
Unfortunately I don't know how this would work for a variable number of parameters. Hopefully somebody with better meta programming skills can clear this blunder.