On Tue, May 21, 2019 at 7:55 AM Jim via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> Hi,
>
> Question: How to call foo.x in @safe code ?
>
@safe:
interface Base
{
   void setup();
}

interface FeatureX
{
   void x();
}

interface FeatureY
{
   void y();
}


class Foo: Base, FeatureX
{
   void setup(){};
   void x(){};
}

D castHelper(D, S)(S s) @trusted
if ((is(S == interface) || is(S == class)) && (is(D == interface) || is(D
== class)))
{
    import std.traits;
    D d = cast(D)s;
    if (d) return d;
    else assert(0, fullyQualifiedName!S ~ " does not inherit from " ~
fullyQualifiedName!D);
}

void main()
{
   auto foo = new Foo(); // This would be the result of a factory

   castHelper!FeatureX(foo).x;
}

Reply via email to