Thread overview | |||||
---|---|---|---|---|---|
|
January 29, 2018 cast problem about alias this | ||||
---|---|---|---|---|
| ||||
class Parent { int x; alias x this; } class Child : Parent { } void main() { Parent p = new Child; Child c = cast(Child)p; // cannot cast `int` to `Child` } In this code, I got a compile error. How can I cast p to Child? |
January 29, 2018 Re: cast problem about alias this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sobaya | On 1/29/18 2:40 PM, Sobaya wrote: > class Parent { > int x; > alias x this; > } > > class Child : Parent { > } > > void main() { > Parent p = new Child; > Child c = cast(Child)p; // cannot cast `int` to `Child` > } > > In this code, I got a compile error. > How can I cast p to Child? I'm sure I've seen this bug reported somewhere before. Ah yes: https://issues.dlang.org/show_bug.cgi?id=6777 What you can do is cast to Object first. -Steve |
January 29, 2018 Re: cast problem about alias this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 29 January 2018 at 19:59:31 UTC, Steven Schveighoffer wrote:
> On 1/29/18 2:40 PM, Sobaya wrote:
>> class Parent {
>> int x;
>> alias x this;
>> }
>>
>> class Child : Parent {
>> }
>>
>> void main() {
>> Parent p = new Child;
>> Child c = cast(Child)p; // cannot cast `int` to `Child`
>> }
>>
>> In this code, I got a compile error.
>> How can I cast p to Child?
>
> I'm sure I've seen this bug reported somewhere before. Ah yes: https://issues.dlang.org/show_bug.cgi?id=6777
>
> What you can do is cast to Object first.
>
> -Steve
I see.
I will continue by casting to Object for now.
Hope the bug is fixed soon.
Thanks!
- Sobaya
|
Copyright © 1999-2021 by the D Language Foundation