www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13728] New: std.conv.to for POD structs

https://issues.dlang.org/show_bug.cgi?id=13728

          Issue ID: 13728
           Summary: std.conv.to for POD structs
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

You can convert 3 strings into a fixed-size array of length 3 using
std.conv.to:

void main() {
    import std.conv: to;
    string[] data = ["1", "2", "3"];
    auto foo = data.to!(int[3]);
}



So to simplify some code I'd like std.conv.to to support conversions to POD
structs too (without constructors):

void main() {
    import std.conv: to;
    string[] data = ["1.5", "2", "3"];
    static struct Foo { double a; uint b, c; }
    auto foo = data.to!Foo;
}



That is similar to:

void main() {
    import std.conv: to;
    string[] data = ["1.5", "2", "3"];
    static struct Foo { double a; uint b, c; }
    auto foo = Foo(data[0].to!(typeof(Foo.tupleof[0])),
                   data[1].to!(typeof(Foo.tupleof[1])),
                   data[2].to!(typeof(Foo.tupleof[2])));
}



Similar code for std.typecons.tuples could be supported:

void main() {
    import std.conv: to;
    import std.typecons: Tuple;
    string[] data = ["1.5", "2", "3"];
    alias Foo = Tuple!(double,"a", uint,"b", uint,"c");
    auto foo = data.to!Foo;
}

--
Nov 13 2014