www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9089] New: Very restrictive Tuple constructor

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9089

           Summary: Very restrictive Tuple constructor
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 27 2012
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9089


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



09:44:19 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 02 2012