October 20, 2011 Re: to!() conversion between objects | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
| |||
October 20, 2011 Re: to!() conversion between objects | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Piotr Szturmaj | 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 | |||
October 20, 2011 Re: to!() conversion between objects | ||||
|---|---|---|---|---|
| ||||
Posted in reply to kenji hara | 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
| |||
October 21, 2011 Re: to!() conversion between objects | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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 > | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply