Cast Object - get null

Namespace rswhite4 at googlemail.com
Wed Apr 18 10:37:25 PDT 2012


I take your first example and the relevant class part looks now 
like this:
class Vector2D(T) {
public:
	T x;
	T y;

	this() { }

	/**
	*
	*/
	static Vector2D!(T) opCall(U, V)(U x, V y) {
		Vector2D!(T) vec = new Vector2D!(T)();
		vec.x = cast(T) x;
		vec.y = cast(T) y;

		return vec;
	}
	
	/**
	*
	*/
	static Vector2D!(T) opCall(U)(const Vector2D!(U) vec) {
		assert(vec !is null);

		return Vector2D!(T)(vec.x, vec.y);
	}
	
	U opCast(U)() const
		if (is(Unqual!U == Vector2D!byte) ||
			is(Unqual!U == Vector2D!ubyte) ||
			is(Unqual!U == Vector2D!short) ||
			is(Unqual!U == Vector2D!ushort) ||
			is(Unqual!U == Vector2D!int) ||
			is(Unqual!U == Vector2D!uint) ||
			is(Unqual!U == Vector2D!long) ||
			is(Unqual!U == Vector2D!ulong) ||
			is(Unqual!U == Vector2D!float) ||
			is(Unqual!U == Vector2D!double) ||
			is(Unqual!U == Vector2D!real))
	{
		U v = new U(this.x, this.y);
	}

As you see i have a static opCall.
But now i get this compiler errors:
cast.d(309): Error: template instance opCast!(Object) 
opCast!(Object) does not m
atch template declaration opCast(U) if (is(Unqual!(U) == 
Vector2D!(byte)) || is(
Unqual!(U) == Vector2D!(ubyte)) || is(Unqual!(U) == 
Vector2D!(short)) || is(Unqu
al!(U) == Vector2D!(ushort)) || is(Unqual!(U) == Vector2D!(int)) 
|| is(Unqual!(U
) == Vector2D!(uint)) || is(Unqual!(U) == Vector2D!(long)) || 
is(Unqual!(U) == V
ector2D!(ulong)) || is(Unqual!(U) == Vector2D!(float)) || 
is(Unqual!(U) == Vecto
r2D!(double)) || is(Unqual!(U) == Vector2D!(real)))
cast.d(309): Error: function expected before (), not 
vs2.opCast!(Object) of type
  void
cast.d(309): Error: template instance opCast!(Object) 
opCast!(Object) does not m
atch template declaration opCast(U) if (is(Unqual!(U) == 
Vector2D!(byte)) || is(
Unqual!(U) == Vector2D!(ubyte)) || is(Unqual!(U) == 
Vector2D!(short)) || is(Unqu
al!(U) == Vector2D!(ushort)) || is(Unqual!(U) == Vector2D!(int)) 
|| is(Unqual!(U
) == Vector2D!(uint)) || is(Unqual!(U) == Vector2D!(long)) || 
is(Unqual!(U) == V
ector2D!(ulong)) || is(Unqual!(U) == Vector2D!(float)) || 
is(Unqual!(U) == Vecto
r2D!(double)) || is(Unqual!(U) == Vector2D!(real)))
cast.d(309): Error: function expected before (), not 
vf.opCast!(Object) of type
void

It seems that opCast and opEquals don't like each other.
Line 309 is
if (vs2 == vf) {


More information about the Digitalmars-d-learn mailing list