interface reference not compatible to Object?
Jari-Matti Mäkelä
jmjmak at utu.fi.invalid
Mon Oct 23 10:22:27 PDT 2006
Lionello Lunesu wrote:
> Jari-Matti Mäkelä wrote:
>> Walter Bright wrote:
>>> Frank Benoit (keinfarbton) wrote:
>>>> Is this the intended behaviour? If *every* object in D is a Object,
>>>> then
>>>> every interface reference refers to an Object also.
>>>> This said, the above should compile?
>>> Replace:
>>> i.toHash();
>>> with:
>>> (cast(Object)i).toHash();
>>> and it will work, *provided* that i is actually an instance of an
>>> Object. If it is not, for example if it came from some COM DLL, the cast
>>> will fail.
>>
>> Is there much hope that the compiler will make the cast implicitly in
>> the future releases?
>
> An interface's vftbl doesn't have toString and others. It does however
> have some RTTI which means a (dynamic_) cast can be used to find the
> pointer to the Object class, but only if it is part of an Object.
>
> This information is only available at run-time. It should not be
> implicit, since it might not succeed.
Yeah, I know that. But from a *nix users point of view this is annoying.
As there are no COM-objects available, basically everything is an
Object. Some consider that explicit typecast is an ugly hack. Java wins
D here 10-0. If I make programs with 50+ classes/interfaces, I have to
write at least 200 of those typecasts. Still I cannot get any speed
advantage over Java since it caches the lookups.
Since D syntax does not distinguish between interfaces and classes when
implementing/extending:
interface b;
class c;
class a : b, c
versus
interface b
class c
class a implements b extends c
it becomes hard to look for correct signatures. Basically I have to
print the signatures to paper, recursively grep the source tree or
compile every now and then, fix something and try again since there are
no good IDE's for D with syntax checking. Ok, I might need to write 30%
less lines with D but it takes 400% more time to figure out those types :(
More information about the Digitalmars-d
mailing list