Casting to interface not allowed in @safe code?

rumbu rumbu at rumbu.ro
Tue May 21 07:04:27 UTC 2019


On Tuesday, 21 May 2019 at 05:51:30 UTC, Jim wrote:
> Hi,
>
> consider this:
>
> interface Base
> {
>   void setup();
> }
>
> interface FeatureX
> {
>   void x();
> }
>
> class Foo: Base, FeatureX
> {
>   void setup(){};
>   void x(){};
> }
>
> void main()
> {
>   Base foo = new Foo(); // This would be the result of a 
> factory class
>
>   (cast(FeatureX)foo).x(); // 1
> }
>
> 1) error: casting to interface FeatureX is not allowed in @safe 
> code.
>
> Question: How to call foo.x in @safe code ?

That's because foo is of type Base, not implementing FeatureX.






More information about the Digitalmars-d-learn mailing list