Thread overview
[Issue 8001] New: Alias this takes ownership of explicit cast
Apr 29, 2012
Jesse Phillips
May 02, 2012
Namespace
May 02, 2012
Namespace
Feb 05, 2013
Maksim Zholudev
April 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8001

           Summary: Alias this takes ownership of explicit cast
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: Jesse.K.Phillips+D@gmail.com


--- Comment #0 from Jesse Phillips <Jesse.K.Phillips+D@gmail.com> 2012-04-29 07:41:14 PDT ---
When attempting to downcast a class which uses alias this, the operation fails because the cast is applied to the item aliased.

I think the semantics should be to attempt an implicit cast (alias this), then a cast of the top class, afterwards the alias this item can be casted.

test.d(3): Error: e2ir: cannot cast a.val of type int to type test.B

void main () {
    A a = new B();
    B b = cast(B) a;
}

class A {
    int val;
    alias val this;
}
class B : A {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 02, 2012
On Sunday, 29 April 2012 at 14:40:06 UTC, Jesse Phillips wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=8001
>
>            Summary: Alias this takes ownership of explicit cast
>            Product: D
>            Version: D2
>           Platform: All
>         OS/Version: All
>             Status: NEW
>           Severity: normal
>           Priority: P2
>          Component: DMD
>         AssignedTo: nobody@puremagic.com
>         ReportedBy: Jesse.K.Phillips+D@gmail.com
>
>
> --- Comment #0 from Jesse Phillips <Jesse.K.Phillips+D@gmail.com> 2012-04-29 07:41:14 PDT ---
> When attempting to downcast a class which uses alias this, the operation fails
> because the cast is applied to the item aliased.
>
> I think the semantics should be to attempt an implicit cast (alias this), then
> a cast of the top class, afterwards the alias this item can be casted.
>
> test.d(3): Error: e2ir: cannot cast a.val of type int to type test.B
>
> void main () {
>     A a = new B();
>     B b = cast(B) a;
> }
>
> class A {
>     int val;
>     alias val this;
> }
> class B : A {}

Workaround:

import std.stdio;

class A {
   int val;

   alias val this;
}

class B : A { }

void main () {
   A a = new B();
   B* b = cast(B*) &a;
}

But i have no idea how this can work implicit as long as this bug isn't fixed.
Maybe it can work with an opCast in B, which cast first to B* and then to B.
May 02, 2012
This works fine:

[code]
import std.stdio;

class A {
	int val;

	alias val this;
	
	T opCast(T : Object)() {
		return to!(T)(this);
	}
}

class B : A {

}

T to(T : Object, U : Object)(const U obj) {
	return *(cast(T*) &obj);
}

void main () {
	A a = new B();
	a.val = 42;
	
	writefln("a.val: %d", a.val);

	B* b = cast(B*) &a;
	writefln("*b.val: %d", b.val);

	B b1 = to!(B)(a);
	writefln("b1.val: %d", b1.val);
	
	B b2 = cast(B) a;
	writefln("b2.val: %d", b2.val);
}
[/code]
February 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8001


Maksim Zholudev <maximzms@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maximzms@gmail.com


--- Comment #1 from Maksim Zholudev <maximzms@gmail.com> 2013-02-05 07:57:31 PST ---
May be connected with http://d.puremagic.com/issues/show_bug.cgi?id=6777

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------