std.typecons.Proxy + inheritance breaks cast'ing to inherited type

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 17 00:14:37 PDT 2015


On Tuesday, 17 March 2015 at 05:32:49 UTC, Ali Çehreli wrote:
> On 03/16/2015 04:59 PM, Lukasz Wrzosek wrote:
>> Bug reported as
>> https://issues.dlang.org/show_bug.cgi?id=14298
>
> Thanks...
>
> I have carried the discussion over to the main newsgroup:
>
>   http://forum.dlang.org/thread/me8e0a$1kp6$1@digitalmars.com
>
> As I mention there, there is a workaround: Add a catch-all 
> opCast to the class in question, which can simply forward to 
> the all-powerful std.conv.to:
>
>     T opCast(T)()
>     {
>         import std.conv;
>         return this.to!T;
>     }
>
> Now it compiles and works as expected.
>
> Ali

That will throw an exception if the conversion can't be done 
instead of just returning null, won't it? Why can't you do this 
instead?

t opCast(t)()
if (is(typeof(cast(T)this)))
{
     return cast(T)this;
}


More information about the Digitalmars-d-learn mailing list