Re: alias this & cast
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
Re: alias this & cast
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 + andre via Digitalmars-d-learn 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); }
Re: alias this & cast
V Thu, 11 Sep 2014 11:40:05 + andre via Digitalmars-d-learn 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); > }
alias this & cast
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); }
alias this : cast turns into endless loop
Trying with U opCast(U : Object)() { return new U(); } work fine. It isn't a solution, but i don't understand why this works and the following not: U opCast(U : Object)() { return cast(U) this._obj; } I hope really some of you can explain that to me and help me to fix it. And forgive me the old senseless thread title. ;)