Using std.conv.to with std.typecons.Typedef

Saurabh Das via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 11 04:15:55 PST 2016


On Monday, 11 January 2016 at 12:01:30 UTC, Tobi G. wrote:
> On Monday, 11 January 2016 at 08:03:19 UTC, Saurabh Das wrote:
>>
>> How can I get std.conv to understand std.typecons.Typedef?
>
> You can do something like this:
>
> QuestionId q = to!(TypedefType!QuestionId)("43");
>
>> In general, is there a better solution to orthogonal types 
>> than Typedef?
>
> Typedef is a reasonably solution, for this in my opinion.
>
> togrue

Oh excellent. Yes that works for a standalone conversion.  Do you 
know how I can use this with std.csv?

import std.typecons;
alias QuestionId = Typedef!(long, long.init, "QuestionId");
alias StudentId = Typedef!(long, long.init, "StudentId");

struct MyStuff
{
     QuestionId q;
     StudentId s;
}

void main()
{
     import std.csv, std.stdio, std.algorithm, std.range;
     
File("file.csv").byLine.joiner("\n").csvReader!(MyStuff)(null).array;
}

This doesn't work. But if MyStuff is defined as: struct MyStuff { 
int q, s; }, then it works.

Any ideas?




More information about the Digitalmars-d-learn mailing list