On Monday, 3 December 2012 at 13:50:45 UTC, Jacob Carlborg wrote:
On 2012-12-03 11:20, Paulo Pinto wrote:

When coding against interfaces, it is always a good decision not to try
to get back the object.

The main idea about using interfaces in first place is to have behavior independent of whatever class might implement it. If someone is trying to get back the implementation of a given interface, it is because the
design is not sound in first place.

Well, one would might think that this would work:

interface I {}

class A : I {}

void main ()
{
    I i = new A;
    Object o = i;
}

But it doesn't.

Object does not implement interface I, so why should interface instance be implicitly converted to it? It even should not be implicitly convertible to class if that class implements interface. However it is possible to use cast.

TDPL and dlang.org say almost nothing about it, but current investigated dmd behavior (http://dpaste.dzfl.pl/84637416) makes sense: it allows convenient conversion when it should happen, protects from poor implicit conversion which should not happen, but still allows to do what you want if you assume you are right. If you not, dmd punish you with segfault.


Reply via email to