digitalmars.D - to!() conversion between objects
- Piotr Szturmaj (13/13) Oct 19 2011 I have written a simple conversion template for tuples, structs and clas...
- bearophile (4/5) Oct 19 2011 Do you have some use case to show me?
- Piotr Szturmaj (13/16) Oct 19 2011 class C
- Robert Jacques (2/20) Oct 19 2011 So C's s field maps to S's f field, not it's s field? That seems unintui...
- Piotr Szturmaj (6/33) Oct 20 2011 This isn't name to name mapping but field to field mapping.
- kenji hara (6/39) Oct 20 2011 I think std.conv.to should provide the *safe natural conversion*.
- Andrei Alexandrescu (3/6) Oct 20 2011 I agree. We could, however, add std.conv.structuralCast.
- Gor Gyolchanyan (4/11) Oct 21 2011 That would be great! that ides evaluates to:
- bearophile (5/21) Oct 19 2011 This is an usage example, it isn't an use case.
- Piotr Szturmaj (26/45) Oct 20 2011 Okay, as I said in other post it is only a complement to static
- bearophile (5/14) Oct 20 2011 See here the answer 23 by Andrei Alexandrescu to a sub-request of mine:
- Piotr Szturmaj (3/15) Oct 20 2011 "Too much magic" argument is for dmd implementation, but it may be
- Piotr Szturmaj (4/6) Oct 19 2011 This is only the part to complement universal range/array to
- Robert Jacques (2/9) Oct 19 2011 My proposed improvement to std.variant handles duck-typing style convers...
I have written a simple conversion template for tuples, structs and classes: https://gist.github.com/1299046 The problem is that it may conflict with to!Class1(Class2) template. This template does dynamic cast and throw exception when the target is null and source is non null. Why implement that in std.conv? Why not enforce(cast(Class1)Class2_object) or maybe create something like DynamicCast!T? Currently, in my private repo I added a constraint so dynamic cast template is chosen only if tupleof lengths are different. If they are the same then conversion is performed on each field, but I think it may be sometimes ambiguous. Is that field-by-field conversion welcome in Phobos at all? Thanks
Oct 19 2011
Piotr Szturmaj:I have written a simple conversion template for tuples, structs and classes:Do you have some use case to show me? Bye, bearophile
Oct 19 2011
bearophile wrote:Piotr Szturmaj:class C { int i; string s; } struct S { string s; float f; } auto c = to!C(S("5", 2.5f)); assert(c.i == 5 && c.s == "2.5");I have written a simple conversion template for tuples, structs and classes:Do you have some use case to show me?
Oct 19 2011
On Wed, 19 Oct 2011 14:16:31 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:bearophile wrote:So C's s field maps to S's f field, not it's s field? That seems unintuitive and bug prone.Piotr Szturmaj:class C { int i; string s; } struct S { string s; float f; } auto c = to!C(S("5", 2.5f)); assert(c.i == 5 && c.s == "2.5");I have written a simple conversion template for tuples, structs and classes:Do you have some use case to show me?
Oct 19 2011
Robert Jacques wrote:On Wed, 19 Oct 2011 14:16:31 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:This isn't name to name mapping but field to field mapping. Better example would be: alias Tuple!(int, string) IntString; auto t = to!IntString(S("5", 2.5f)); assert(t[0] == 5 && t[1] == "2.5");bearophile wrote:So C's s field maps to S's f field, not it's s field? That seems unintuitive and bug prone.Piotr Szturmaj:class C { int i; string s; } struct S { string s; float f; } auto c = to!C(S("5", 2.5f)); assert(c.i == 5 && c.s == "2.5");I have written a simple conversion template for tuples, structs and classes:Do you have some use case to show me?
Oct 20 2011
2011/10/20 Piotr Szturmaj <bncrbme jadamspam.pl>:Robert Jacques wrote:I'm agree with bearophile.On Wed, 19 Oct 2011 14:16:31 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:bearophile wrote:So C's s field maps to S's f field, not it's s field? That seems unintuitive and bug prone.Piotr Szturmaj:class C { int i; string s; } struct S { string s; float f; } auto c = to!C(S("5", 2.5f)); assert(c.i == 5 && c.s == "2.5");I have written a simple conversion template for tuples, structs and classes:Do you have some use case to show me?This isn't name to name mapping but field to field mapping.I think std.conv.to should provide the *safe natural conversion*. Field-to-field conversion seems not natural, and it is called 'Structural conversion'. Kenji Hara
Oct 20 2011
On 10/20/11 6:53 AM, kenji hara wrote:I think std.conv.to should provide the *safe natural conversion*. Field-to-field conversion seems not natural, and it is called 'Structural conversion'.I agree. We could, however, add std.conv.structuralCast. Andrei
Oct 20 2011
That would be great! that ides evaluates to: --boilerplate; On Thu, Oct 20, 2011 at 7:02 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:On 10/20/11 6:53 AM, kenji hara wrote:I think std.conv.to should provide the *safe natural conversion*. Field-to-field conversion seems not natural, and it is called 'Structural conversion'.I agree. We could, however, add std.conv.structuralCast. Andrei
Oct 21 2011
Piotr Szturmaj:This is an usage example, it isn't an use case. And it looks a bit messy. Bye, bearophileDo you have some use case to show me?class C { int i; string s; } struct S { string s; float f; } auto c = to!C(S("5", 2.5f)); assert(c.i == 5 && c.s == "2.5");
Oct 19 2011
bearophile wrote:Piotr Szturmaj:Okay, as I said in other post it is only a complement to static tuple/struct mapping from ranges. Here are the use cases: -- for SQL: // conversion is used internally by db client implementation auto result = sqlConn.query!S("SELECT 5, '2.5'"); auto row = result.front(); assert(row.s == "5" && row.f == 2.5f); -- for CSV: alias Tuple!(int, string, int) IdNameAge; foreach (csvLine; csvFile.byCSVLine) { auto line = to!IdNameAge(splitter(csvLine, ';')); // line here is a tuple converted from runtime csv fields } I'm going to argue that Phobos needs universal construction for handling static mapping of runtime data. This is done with runtime checks on field count and each field is converted using actual to!() functions. It may be used by database client writers. Conversion function should look like this: T toImpl(T, Range)(Range r) { // TODO: converts to tuple, struct or class } The original toImpl posted by me is only a complement to the above. I think it may be helpful and avoid converting each field manually.This is an usage example, it isn't an use case. And it looks a bit messy.Do you have some use case to show me?class C { int i; string s; } struct S { string s; float f; } auto c = to!C(S("5", 2.5f)); assert(c.i == 5&& c.s == "2.5");
Oct 20 2011
Piotr Szturmaj:-- for CSV: alias Tuple!(int, string, int) IdNameAge; foreach (csvLine; csvFile.byCSVLine) { auto line = to!IdNameAge(splitter(csvLine, ';')); // line here is a tuple converted from runtime csv fields }See here the answer 23 by Andrei Alexandrescu to a sub-request of mine: http://d.puremagic.com/issues/show_bug.cgi?id=6365#c23 Bye, bearophile
Oct 20 2011
bearophile wrote:Piotr Szturmaj:"Too much magic" argument is for dmd implementation, but it may be implemented in std.conv.-- for CSV: alias Tuple!(int, string, int) IdNameAge; foreach (csvLine; csvFile.byCSVLine) { auto line = to!IdNameAge(splitter(csvLine, ';')); // line here is a tuple converted from runtime csv fields }See here the answer 23 by Andrei Alexandrescu to a sub-request of mine: http://d.puremagic.com/issues/show_bug.cgi?id=6365#c23
Oct 20 2011
Piotr Szturmaj:This is only the part to complement universal range/array to tuple/struct/class conversion. It may be useful in mapping runtime fields like database rows or CSV lines onto objects. I think Phobos needs a common solution for that.I have written a simple conversion template for tuples, structs and classes:
Oct 19 2011
On Wed, 19 Oct 2011 14:31:20 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:My proposed improvement to std.variant handles duck-typing style conversions between values.Piotr Szturmaj:This is only the part to complement universal range/array to tuple/struct/class conversion. It may be useful in mapping runtime fields like database rows or CSV lines onto objects. I think Phobos needs a common solution for that.I have written a simple conversion template for tuples, structs and classes:
Oct 19 2011