Jump to page: 1 2
Thread overview
to!() conversion between objects
Oct 19, 2011
Piotr Szturmaj
Oct 19, 2011
bearophile
Oct 19, 2011
Piotr Szturmaj
Oct 20, 2011
Robert Jacques
Oct 20, 2011
Piotr Szturmaj
Oct 20, 2011
kenji hara
Oct 21, 2011
Gor Gyolchanyan
Oct 20, 2011
bearophile
Oct 20, 2011
Piotr Szturmaj
Oct 20, 2011
bearophile
Oct 20, 2011
Piotr Szturmaj
Oct 19, 2011
Piotr Szturmaj
Oct 20, 2011
Robert Jacques
October 19, 2011
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
October 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
October 19, 2011
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");
October 19, 2011
> 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.
October 20, 2011
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.
October 20, 2011
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.
October 20, 2011
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
October 20, 2011
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");
October 20, 2011
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.
October 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
« First   ‹ Prev
1 2