Thread overview
Issue with casting types
May 17, 2016
Thorsten Sommer
May 17, 2016
ag0aep6g
May 17, 2016
Thorsten Sommer
May 17, 2016
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...

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!?

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...


Best regards,
Thorsten
May 17, 2016
On 05/17/2016 02:24 PM, 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...

You've got an infinite recursion there.

`A a2 = to!A(b1);` calls `b1.opCast!A()` which calls `to!A(this)` which is the very same call as `to!A(b1)`, so it calls `opCast!A()` again, and so on until the stack is exhausted and the program crashes.

> 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!?

There's an issue on this: https://issues.dlang.org/show_bug.cgi?id=6777

I don't know if there's a good reason for the current behavior. Looks silly to me.

> 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...

You can use the reinterpreting style of cast to circumvent features like alias this and opCast:
----
A a2 = * cast(A*) &b1;
----
May 17, 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
May 17, 2016
On 5/17/16 8:59 AM, Steven Schveighoffer wrote:
> I think you need to avoid it for now. Please file an issue.

I see from ag0aep6g, that there is already an issue. I updated it.

-Steve
May 17, 2016
On Tuesday, 17 May 2016 at 13:13:54 UTC, Steven Schveighoffer wrote:
> On 5/17/16 8:59 AM, Steven Schveighoffer wrote:
>> I think you need to avoid it for now. Please file an issue.
>
> I see from ag0aep6g, that there is already an issue. I updated it.
>
> -Steve

Thanks ag0aep6g and Steve for the fast help :) The D community is great :)
May 17, 2016
On 5/17/16 10:19 AM, Thorsten Sommer wrote:
> On Tuesday, 17 May 2016 at 13:13:54 UTC, Steven Schveighoffer wrote:
>> On 5/17/16 8:59 AM, Steven Schveighoffer wrote:
>>> I think you need to avoid it for now. Please file an issue.
>>
>> I see from ag0aep6g, that there is already an issue. I updated it.
>>
>
> Thanks ag0aep6g and Steve for the fast help :) The D community is great :)

Someone identified a workaround in the bug report:

Object obj = b1;
auto a2 = cast(A)obj;

Very ugly, but it should work.

-Steve