www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias this & cast

reply "andre" <andre s-e-a-p.de> writes:
Hi,

I am 80% sure, the failing assertion is correct but please have a 
look.
Second assertion fails.

Kind regards
André

class A{}

class B{}

class C : B
{
	A a;
	alias a this;
	
	this()
	{
		a = new A();
	}
}

void main()
{
	B b = new C();

	// OK
	assert(cast(C)b);
	
	// fails
	assert(cast(A)b);	
}
Sep 11 2014
parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
V Thu, 11 Sep 2014 11:40:05 +0000
andre via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
napsáno:

 Hi,
 
 I am 80% sure, the failing assertion is correct but please have a 
 look.
No it is not assert(cast(A)cast(C)b); // this is OK b is B so it does not know about having alias to A;
 Second assertion fails.
 
 Kind regards
 André
 
 class A{}
 
 class B{}
 
 class C : B
 {
 	A a;
 	alias a this;
 	
 	this()
 	{
 		a = new A();
 	}
 }
 
 void main()
 {
 	B b = new C();
 
 	// OK
 	assert(cast(C)b);
 	
 	// fails
 	assert(cast(A)b);	
 }
Sep 11 2014
parent reply "andre" <andre s-e-a-p.de> writes:
I am not sure. b is C but everything not in super class B is 
hidden.
Using cast I can cast b to a full C.

The cast "cast(C)b" has the same information about b like the 
cast "cast(A)b": The memory area of b knows compatitibility to C 
and also the alias.

For me, using alias this, the object b has 3 represenations: A, B 
and C. It is a matter of steps.

Kind regards
André

On Thursday, 11 September 2014 at 11:53:30 UTC, Daniel Kozak via 
Digitalmars-d-learn wrote:
 V Thu, 11 Sep 2014 11:40:05 +0000
 andre via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 napsáno:

 Hi,
 
 I am 80% sure, the failing assertion is correct but please 
 have a look.
No it is not assert(cast(A)cast(C)b); // this is OK b is B so it does not know about having alias to A;
 Second assertion fails.
 
 Kind regards
 André
 
 class A{}
 
 class B{}
 
 class C : B
 {
 	A a;
 	alias a this;
 	
 	this()
 	{
 		a = new A();
 	}
 }
 
 void main()
 {
 	B b = new C();
 
 	// OK
 	assert(cast(C)b);
 	
 	// fails
 	assert(cast(A)b);	
 }
Sep 11 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/11/2014 09:18 AM, andre wrote:

 I am not sure. b is C but everything not in super class B is hidden.
 Using cast I can cast b to a full C.

 The cast "cast(C)b" has the same information about b like the cast
 "cast(A)b": The memory area of b knows compatitibility to C and also the
 alias.
That's only because 'b' really is a C.
 For me, using alias this, the object b has 3 represenations: A, B and C.
Correct but it cannot be known whether any B is an A: void foo(B b) { // ... } Can that 'b' used as an A? Who knows... It may be desirable that the compiler did static code analysis and saw that the 'b' in your code is always a C, therefore can be casted to an A. Compilers do not and most of the time cannot do that. Consider one line added to you program:
 class A{}

 class B{}

 class C : B
 {
     A a;
     alias a this;

     this()
     {
         a = new A();
     }
 }

 void main()
 {
     B b = new C();
Add this: takesBbyReference(b); Now nobody knows whether the object has changed to something other than C. For example: class Z : B {} void takesBbyReference(ref B b) { b = new Z; } Now the first assert fails as well:
     assert(cast(C)b);
Ali
Sep 11 2014