Bug 9089 – Very restrictive Tuple constructor

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-27T19:26:00Z
Last change time
2015-01-13T13:25:22Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

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.
Comment #2 by k.hara.pg — 2015-01-13T13:25:22Z
It's properly fixed in 2.063, in the PR: https://github.com/D-Programming-Language/phobos/pull/1243 With an IFTI improvement issue 9885. https://github.com/D-Programming-Language/dmd/pull/1847