Community

Hi everybody
I wrote the following code to be able to write unit tests for interfaces.
--------------------------------------------------------------
module test;
import std.stdio;
interface I
{
void doSomething();
}
class Iimplementation : I
{
void doSomething()
{
writefln("Hello world!");
}
}
typedef I function() FactoryType;
Iimplementation Factory()
{
return new Iimplementation();
}
void main()
{
auto factory = &Factory;
auto ifactory = cast(FactoryType)factory;
I i = factory();
i.doSomething(); // Prints 'Hello world!'
i = ifactory();
i.doSomething(); // Prints 'InterfaceClassTest.Iimplementation' <<< ??
}
--------------------------------------------------------------
compiled using dmd 1.015 on windows
The problem is marked in the source. When i create the instance using
factory the program works as expected, but when i cast it just prints
garbage(?).
Is this a normal behavior? If it is, how can i create an instance of
an interface using a factory?

Reply to luke,
> But i think the type should be valid because it implements the
> interface B.
>
> Or am i wrong about the whole thing?
>
this code IS valid:
interface A{}
interface B: A{}
class C : B{}
B Factory()
{
return new C();
}
auto i = cast(A)Factory();
Factory news a C, casts it to a B, and returns it
then it is cast to an A and assigned.
All OK
the issue is when you coast a function returning an interface to a function
returning another interface.
When you do this the cast from the original type to the new type never happens,
the reference is copied directly. However because of how interfaces are implemented
this gives incorrect results. (The details were brought up in a thread about
4 or 5 days ago)