std.conv.to

harakim harakim at gmail.com
Fri Jun 17 18:00:44 UTC 2022


On Friday, 17 June 2022 at 12:53:53 UTC, bauss wrote:
> Just add a constructor to your type that takes the value of 
> what you want to convert.
>
> Working example:
>
> ```d
> struct Foo
> {
>     int value;
>
>     this(int v)
>     {
>         value = v;
>     }
>
>     this(string v)
>     {
>         this(to!int(v));
>     }
> }
>
> void main()
> {
>     auto foo = to!Foo("123");
>
>     writeln(foo.value);
> }
> ```

D is full of good surprises. Thanks.


More information about the Digitalmars-d-learn mailing list