<div class="gmail_quote">On 7 June 2012 17:09, Jens Mueller <span dir="ltr"><<a href="mailto:jens.k.mueller@gmx.de" target="_blank">jens.k.mueller@gmx.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">Manu wrote:<br>
> Seriously?<br>
><br>
> I perceive to!T and cast(T) as fundamentally different operations. How can<br>
> opCast correctly perform the role of to! ?<br>
> cast() is a low level type cast, to! implies a conversion of some kind. If<br>
> you have a pointer type, I assume cast to operate on the pointer, and to!<br>
> to perform a conversion of the thing it represents.<br>
><br>
> to!int("12345")  or  cast(int)"12345"<br>
> to!JSONValue(obj)  or  cast(JSONValue)obj<br>
> cast(string)obj  <- appears to be a primitive cast operator, communicates<br>
> nothing to me about the expected format of the string produced<br>
><br>
> The cast approach feel really wrong to me...<br>
><br>
> I need to do conversion between some fairly complex types. to! feels<br>
> appropriate, but cast() just seems weird...<br>
> Have I missed something?<br>
<br>
</div></div>I think the passage you are referring to applies only when converting<br>
from a user defined type to a built-in type.<br>
If you have user defined types only the right approach is to define a<br>
constructor in the target type I think.<br>
Something like this:<br>
<br>
struct A {}<br>
struct B {<br>
        this(A a)<br>
        {<br>
        }<br>
}<br>
<br>
to!B(A); // will use B's constructor<br>
<br>
I don't know what to do if you have no control over B (besides opCast or<br>
providing a specialized to! template).<br></blockquote><div><br></div><div>I have no control over B.</div><div>I thought the idea of toImpl() was to avoid having to specialise to! ?</div><div>I generally liked the idea, except the name toImpl seems odd (what's 'implicit' about an explicit conversion function?)</div>
</div>