Casting to interface not allowed in @safe code?
    rumbu 
    rumbu at rumbu.ro
       
    Tue May 21 07:33:17 UTC 2019
    
    
  
On Tuesday, 21 May 2019 at 07:16:49 UTC, Jim wrote:
> On Tuesday, 21 May 2019 at 07:04:27 UTC, rumbu wrote:
>> On Tuesday, 21 May 2019 at 05:51:30 UTC, Jim wrote:
>>
>> That's because foo is of type Base, not implementing FeatureX.
>
> Right, Base isn't implementing FeatureX, but foo is really a Foo
That's your knowledge, for the compiler foo is really a Base, as 
written in your own code.
The only way to tell the compiler that you safely assume that foo 
is really a Foo, is to mark code as @trusted, not @safe.
interface Base
{
   void setup();
}
interface FeatureX
{
   @safe void x();
}
class Foo: Base, FeatureX
{
   void setup(){};
   @safe void x(){};
}
@trusted FeatureX imsureitsfeaturex(Base b)
{
   return cast(FeatureX)b;
}
@safe void main()
{
   Base foo = new Foo(); // This would be the result of a factory 
class
   imsureitsfeaturex(foo).x(); // 1
}
    
    
More information about the Digitalmars-d-learn
mailing list