cannot cast
Namespace
rswhite4 at googlemail.com
Sat Apr 28 07:21:30 PDT 2012
I finished my Ref/NotNull struct, but i've got a problem:
If i try to cast the class, which should implicit convert to
Ref!(Type) with alias this, i get the following error message:
"cannot cast a1.getRef("Ref.d",72u) of type Ref!(A) to type
RefTest.Ref.__unittest1.B"
Can someone explain that to me or help me with it?
It seems that alias this replacement isn't smart enough to
distinguish, if it has to convert.
[code]
import std.conv : to;
struct Ref(T : Object) {
private:
T _obj;
public:
@disable
this();// { }
@disable
this(typeof(null));// { }
this(T obj) {
assert(obj !is null, "Object is null!");
this._obj = obj;
}
@property
inout(T) access() inout {
assert(this._obj !is null, "Access: Object is null!");
return this._obj;
}
//alias access this; // print "Stackoverflow" or "recursive
expansion"
}
mixin template TRef(T : Object) {
final Ref!(T) getRef(string file = __FILE__, size_t line =
__LINE__) in {
assert(this !is null, "Object is null! @ " ~ file ~ " in Line "
~ to!(string)(line) ~ ".");
} body {
return Ref!(T)(this);
}
final Ref!(const T) getRef(string file = __FILE__, size_t line =
__LINE__) const in {
assert(this !is null, "Object is null! @ " ~ file ~ " in Line "
~ to!(string)(line) ~ ".");
} body {
return Ref!(const T)(this);
}
alias getRef this;
}
unittest {
class A {
mixin TRef!(A);
}
class B : A { }
class C : B { }
A a1 = new B();
A a2 = new C();
B b1 = cast(B) a1; // line 72
}
More information about the Digitalmars-d-learn
mailing list