Behavior of std.conv : to with alias this

João Lourenço jlourenco5691 at gmail.com
Thu May 20 16:19:02 UTC 2021


```d
import std;

void main()
{
     struct Foo {
         string s = "string";
         alias s this;

         string toString() { return s.format!"Foo(%s)"; }
     }
     Foo().to!string.writeln;   // string
     Foo().format!"%s".writeln; // Foo(string)


     struct Bar {
         int s = 3;
         alias s this;

         T opCast(T)() { return 43; }
     }
     Bar().to!int.writeln;      // 3
     (cast(int) Bar()).writeln; // 43


     struct Baz {
         T opCast(T)() { return 43; }
     }
     Bar().to!int.writeln;      // 43
     (cast(int) Bar()).writeln; // 43
}
```

Is this the intended behavior of `std.conv : to`?
Shouldn't it only fallback to the `alias this` if **none** of 
those work?


More information about the Digitalmars-d mailing list