Consider this condensed case from real code:
class A {
protected int x;
void init (int i) {x = i;}
int get() {return x;}
}
class B {
protected int x;
void init (string s) {x = (int) s;}
int get() {return x;}
}
A create_a() {
B b = B();
b->init ("17");
return [object(A)] b;
}
In create_a() I try to cheat and return a B as if it were an A, by
using a type cast. It doesn't work:
/home/mast/foo.pike:19:Soft cast of { B = object(implements
/home/mast/foo()->B) } to { A = object(implements /home/mast/foo()->A) } isn't
a valid cast.
(Side note: This error message doesn't explain why the cast is
invalid. Presumably it's due to the prototype of init(), but unlike
other type errors it doesn't go into detail.)
Let's assume that in this case I know that the type difference of the
init() prototype isn't relevant (e.g. it's a function only used in the
object factory and therefore never called outside create_a).
Shouldn't it be possible to use type casts to make the compiler shut
up? I can do this workaround:
A create_a() {
B b = B();
b->init ("17");
object b2 = b;
return [object(A)] b2;
}
But avoiding that phony extra variable isn't possible; this variant is
still nailed:
A create_a() {
B b = B();
b->init ("17");
return [object(A)] [object] b;
}