www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - to!() conversion between objects

reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
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
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
next sibling parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
bearophile wrote:
 Piotr Szturmaj:

 I have written a simple conversion template for tuples, structs and classes:
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 19 2011
next sibling parent reply "Robert Jacques" <sandford jhu.edu> writes:
On Wed, 19 Oct 2011 14:16:31 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:
 bearophile wrote:
 Piotr Szturmaj:

 I have written a simple conversion template for tuples, structs and classes:
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");
So C's s field maps to S's f field, not it's s field? That seems unintuitive and bug prone.
Oct 19 2011
parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
Robert Jacques wrote:
 On Wed, 19 Oct 2011 14:16:31 -0400, Piotr Szturmaj
 <bncrbme jadamspam.pl> wrote:
 bearophile wrote:
 Piotr Szturmaj:

 I have written a simple conversion template for tuples, structs and
 classes:
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");
So C's s field maps to S's f field, not it's s field? That seems unintuitive and bug prone.
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");
Oct 20 2011
parent reply kenji hara <k.hara.pg gmail.com> writes:
2011/10/20 Piotr Szturmaj <bncrbme jadamspam.pl>:
 Robert Jacques wrote:
 On Wed, 19 Oct 2011 14:16:31 -0400, Piotr Szturmaj
 <bncrbme jadamspam.pl> wrote:
 bearophile wrote:
 Piotr Szturmaj:

 I have written a simple conversion template for tuples, structs and
 classes:
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");
So C's s field maps to S's f field, not it's s field? That seems unintuitive and bug prone.
I'm agree with bearophile.
 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
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
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
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Piotr Szturmaj:

 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");
This is an usage example, it isn't an use case. And it looks a bit messy. Bye, bearophile
Oct 19 2011
parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
bearophile wrote:
 Piotr Szturmaj:

 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");
This is an usage example, it isn't an use case. And it looks a bit messy.
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.
Oct 20 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent Piotr Szturmaj <bncrbme jadamspam.pl> writes:
bearophile wrote:
 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
"Too much magic" argument is for dmd implementation, but it may be implemented in std.conv.
Oct 20 2011
prev sibling parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
 Piotr Szturmaj:

 I have written a simple conversion template for tuples, structs and classes:
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.
Oct 19 2011
parent "Robert Jacques" <sandford jhu.edu> writes:
On Wed, 19 Oct 2011 14:31:20 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:
 Piotr Szturmaj:

 I have written a simple conversion template for tuples, structs and classes:
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.
My proposed improvement to std.variant handles duck-typing style conversions between values.
Oct 19 2011