Cast Object - get null

Namespace rswhite4 at googlemail.com
Wed Apr 18 15:25:03 PDT 2012


>
>> 4. Of course i import it, but it is implicit import if i import
>> std.stdio.
>
> No, it doesn't. If it does, it's an import bug. std.stdio does 
> not publicly
> import std.traits. You need to import it.
>
> - Jonathan M Davis

Believe me, it works fine. So it must be a bug.

For opCast i have now this solution

	U opCast(U)() const {
		static if (is(FieldTypeTuple!(U)[0] == short)) {
			return Vector2D!(short)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == float)) {
			return Vector2D!(float)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == double)) {
			return Vector2D!(double)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == int)) {
			return Vector2D!(int)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == long)) {
			return Vector2D!(long)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == byte)) {
			return Vector2D!(byte)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == ushort)) {
			return Vector2D!(ushort)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == uint)) {
			return Vector2D!(uint)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == ulong)) {
			return Vector2D!(ulong)(this.x, this.y);
		}
		
		static if (is(FieldTypeTuple!(U)[0] == ubyte)) {
			return Vector2D!(ubyte)(this.x, this.y);
		}
		
		return null;
	}

which avoid explicit casts in opEquals.

So that work fine: if (vs2 == vf) {

Only two question: Is that correct behaviour and wouldn't change 
in future versions?
And exist a smarter solution as this? ;)



More information about the Digitalmars-d-learn mailing list