Cast Object - get null

Jonathan M Davis jmdavisProg at gmx.com
Wed Apr 18 10:46:12 PDT 2012


On Wednesday, April 18, 2012 19:37:25 Namespace wrote:
> 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) {

1. opCast doesn't do an implict cast. You'll have to cast to get == to work.

2. If you really have a static opCall, then the new isn't necessary.

3. You need to return from opCast. You're just declaring a local variable.

4. You need to import std.traits, or using Unqual will cause the template 
constraint to fail.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list