Comment #0 by bearophile_hugs — 2012-11-27T19:26:38Z
Is it possible for the Tuple constructors to have value range analysis and accept code like this?
import std.typecons: Tuple;
alias T = Tuple!(short);
void main() {
short x = 1; // OK
T t = T(1); // error
}
Currently in DMD 2.061alpha it gives:
...\dmd2\src\phobos\std\typecons.d(406): Error: cannot implicitly convert expression (_param_0) of type int to short
test.d(5): Error: template instance std.typecons.Tuple!(short).Tuple.__ctor!(int) error instantiating
Comment #1 by andrej.mitrovich — 2012-12-02T09:44:19Z
We could change the ctor to:
this(U...)(U values) if (U.length == Types.length)
{
foreach (i, Unused; Types)
{
static if (isNumeric!(Types[i]))
static assert(isNumeric!(U[i]));
field[i] = to!(Types[i])(values[i]);
}
}
(there are 2 ctors but we can use the same approach)
The isNumeric check is necessary because "to" also converts strings to integrals/floating point. I'm not sure if this would cover all the cases though.