[Issue 13728] New: std.conv.to for POD structs
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Thu Nov 13 02:42:15 PST 2014
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 at puremagic.com
Reporter: bearophile_hugs at 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;
}
--
More information about the Digitalmars-d-bugs
mailing list