Issue with casting types

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 17 05:59:33 PDT 2016


On 5/17/16 8:24 AM, Thorsten Sommer wrote:
> Dear all,
>
> I run into an issue with a simple cast:
> https://dpaste.dzfl.pl/8e7f7c545eb1
>
> I have a base class and a class A with inherit from that base class:
> A <- BaseClass
>
> If the base class contains an "alias this", any casting attempt fails
> because the "alias this" type gets considered. Thus, I defined the
> opCast() to solve this. But now, the program crashes without any error
> or exception...

It's a stack overflow.

obj1.to!OtherObject is going to basically lower to 
cast(OtherObject)obj1. Which then invokes your opCast. Which then 
invokes to, ...

> Why does the casting operation consider the "alias this" at all? I mean:
> The "alias this" type is int. If I try to cast with to!A, obviously int
> does not match A!?

It shouldn't. This is a bug in the language. alias this should only be 
considered if the expression is invalid for the actual type.

> Is there any practical solution? Or should I stop using "alias this" for
> my classes? I like the "alias this" concept. But it caused some issue
> for me...

I think you need to avoid it for now. Please file an issue.

-Steve


More information about the Digitalmars-d-learn mailing list