Casting to interface not allowed in @safe code?

Daniel Kozak kozzi11 at gmail.com
Tue May 21 07:04:52 UTC 2019


On Tue, May 21, 2019 at 7:55 AM Jim via Digitalmars-d-learn <
digitalmars-d-learn at 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;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20190521/6659ba65/attachment.html>


More information about the Digitalmars-d-learn mailing list