to!() conversion between objects

Jonathan M Davis jmdavisProg at gmx.com
Wed Oct 19 12:44:28 PDT 2011


On Wednesday, October 19, 2011 19:47:55 Piotr Szturmaj wrote:
> I have written a simple conversion template for tuples, structs and classes:
> 
> https://gist.github.com/1299046
> 
> The problem is that it may conflict with to!Class1(Class2) template.
> This template does dynamic cast and throw exception when the target is
> null and source is non null. Why implement that in std.conv? Why not
> enforce(cast(Class1)Class2_object) or maybe create something like
> DynamicCast!T?
> 
> Currently, in my private repo I added a constraint so dynamic cast
> template is chosen only if tupleof lengths are different. If they are
> the same then conversion is performed on each field, but I think it may
> be sometimes ambiguous.
> 
> Is that field-by-field conversion welcome in Phobos at all?

The correct way to do create your own conversion is to overload opCast. You 
can then use the cast operator or std.conv.to will use it for you. I think 
that if you want to add some sort of field-by-field conversion, it should be a 
different function entirely.

- Jonathan M Davis


More information about the Digitalmars-d mailing list