www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4168] New: More handy std.conv.to for std.algorithm.map

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

           Summary: More handy std.conv.to for std.algorithm.map
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



Conversion of an array of strings to an array of integers is a common usage of
map (for example the array of strings can come from the splitting of a line
read from a text file), but this code doesn't work:


import std.algorithm: map;
import std.conv: to;
void main() {
    auto number = to!int("10"); // OK
    auto strings = ["10", "20"];
    auto data = map!(to!int)(strings); // ERR
}


DMD 2.045 shows:
test.d(6): Error: template instance to!(int) does not match any template
declaration


You must give both types to the to!, something that you usually don't need to
give (see the 'number' variable in the first example):


import std.algorithm: map;
import std.conv: to;
void main() {
    auto strings = ["10", "20"];
    auto data = map!(to!(int, string))(strings); // OK
}


So to make to!() more handy when used with a map!() Phobos2 can define two to!

This is a test that shows how it can be done (here I have used the name 'foo'
instead of 'to', but in std.conv it will keep its 'to' name):


import std.stdio: writeln;
import std.conv: to;
import std.algorithm: array, map;

template foo(Tout) {
    Tout foo(Tin)(Tin x) {
        return to!(Tout, Tin)(x);
    }
}
Tout foo(Tout, Tin)(Tin x) {
    return to!(Tout, Tin)(x);
}
void main() {
    auto strings = ["10", "20"];
    auto data = map!(foo!int)(strings); // OK
    writeln(foo!int("10"));             // OK
    writeln(foo!(int, string)("20"));   // OK
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4168


David Simcha <dsimcha yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |dsimcha yahoo.com
         Resolution|                            |FIXED



Fixed 2.048.  Your test program now compiles.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 15 2010