Casting by assigning to the right ...

Manfred Nowak svv1999 at hotmail.com
Tue Apr 14 06:44:47 UTC 2020


On Tuesday, 14 April 2020 at 06:10:33 UTC, Steven Schveighoffer 
wrote:
> Note that at this time, only one alias this is allowed per 
> structure.

This restriction falls by the suggested scheme:

```
struct S{
   string data;
     import std.conv: to;
   void castTo( ref int arg){
     arg= to!int( data);
   }
   void castTo( ref string arg){
     arg= data;
   }
}
void main(){
   auto s= S( "42");

   int i;
   s.castTo =i;

   string str;
   s.castTo =str;

     import std.stdio;
   writeln( i, ":", str); // 42:42
}
```


More information about the Digitalmars-d mailing list