www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Cast Object - get null

reply "Namespace" <rswhite4 googlemail.com> writes:
override bool opEquals(Object o) const {
         if (o is null) {
		return false;
	}
	
	writeln(o); // write: cast.Vector2D!(float).Vector2D
	
	Vector2D!(T) vec = cast(Vector2D!(T)) o;
	
	writeln(vec); // write: null
         // ...

It seems the cast fail, but the question is: why?
Apr 18 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
I forget my test context:

void main() {
	alias Vector2D!(short) Vector2s;
	alias Vector2D!(float) Vector2f;
	
	Vector2f vf = Vector2f(23, 42);
	
	Vector2s vs = Vector2s(vf);
	
	writefln("vs.x: %d, vs.y: %d", vs.x, vs.y);
	
	Vector2s vs2 = Vector2s(23, 42);
	
	if (vs2 == vf) {
		writeln("equal");
	}
}

Vector2D is my own class. If i compare vs2 with vs it works fine, 
but if i compare vs or vs2 with vf, the cast fail and i get a 
null-reference. How can i avoid this?
Apr 18 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
Namespace:

 Vector2D is my own class. If i compare vs2 with vs it works fine, 
 but if i compare vs or vs2 with vf, the cast fail and i get a 
 null-reference. How can i avoid this?
I suggest to ask such questions in D.learn. Bye, bearophile
Apr 18 2012
parent "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 18 April 2012 at 16:34:08 UTC, bearophile wrote:
 Namespace:

 Vector2D is my own class. If i compare vs2 with vs it works 
 fine, but if i compare vs or vs2 with vf, the cast fail and i 
 get a null-reference. How can i avoid this?
I suggest to ask such questions in D.learn. Bye, bearophile
If you say so. Then close or delete this thread please.
Apr 18 2012