Using a tuple as a function parameter
Inkrementator
invalid at invalid.org
Fri Nov 22 17:24:08 UTC 2024
On Friday, 22 November 2024 at 16:36:43 UTC, Andrew wrote:
>> Error: variable \`i\` cannot be read at compile time
Tuple access seems to be internally coded as a template (with
unusual syntax) for the reasons already said. `i` has to be known
at compile time for this to work, so a template parameter.
```
string getOrZerothT(Tuple!(string, string, string) tup, int i)()
pure {
//AliasSeq!(a,b,c) = tup.expand;
return tup[i] == "" ? tup[0] : tup[i];
}
string getOrZerothT2(int i)(Tuple!(string, string, string) tup)
pure {
return tup[i] == "" ? tup[0] : tup[i];
}
```
> you could probaly make it compile with an ugly cast, verbose
> switch, but if you know its all the same type you should use an
> array(`foo(T,N)(T[N] arr...)` btw)
Here is something that compiles
```
string getOrZeroth(Tuple!(string, string, string) tup, int i)
pure {
string[tup.length] s = [tup.expand];
return s[i] == "" ? s[0] : s[i];
}
```
More information about the Digitalmars-d-learn
mailing list