Thread overview
Cast Object - get null
Apr 18, 2012
Namespace
Apr 18, 2012
Namespace
Apr 18, 2012
bearophile
Apr 18, 2012
Namespace
April 18, 2012
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?
April 18, 2012
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?
April 18, 2012
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
April 18, 2012
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.