std.conv - custom string to struct conversion ?

monarch_dodra monarchdodra at gmail.com
Thu Aug 1 23:40:51 PDT 2013


On Friday, 2 August 2013 at 05:09:14 UTC, Jonathan M Davis wrote:
> On Friday, August 02, 2013 06:50:01 Douglas Petterson wrote:
>> Is there a way to convert a string to a struct, similarly to 
>> the
>> conversion of a struct to a custom string representation ?
>> 
>> The following code works fine when using format("%s",C) or
>> to!string(C):
>> 
>>      struct sCustomConv
>>      {
>>          string toString()
>>          {
>>              return "My custom representation";
>>          }
>>      }
>> 
>>      sCustomConv C;
>>      string MyBackup = to!string(C);
>> 
>> But I can't find which class operator must be overloaded (or 
>> more
>> simply how-to) to make this possible:
>> 
>>      C = to!sCustomConv(MyBackup);
>
> http://stackoverflow.com/questions/8351245/override-tot-for-used-defined-t-in-d
>
> - Jonathan M Davis

There was a request for enhancement to provide a "fromString" for 
arbitrary types. The idea is that once we have that, then 
functions such as parse or to!S(string) will be generic, and work 
on mostly anything. Unfortunatly, (AFAIK), nobody is working on 
this.

Here is a discussion I opened:
http://forum.dlang.org/thread/gzodptjyzpqnhxctbbuv@forum.dlang.org

...and I just noticed it ends with me saying "I'll try to write a 
DIP then :)" and then not doing it :/


More information about the Digitalmars-d-learn mailing list