Is there any way to define an interface that can implicitly convert to Object?

Basile B. b2.temp at gmx.com
Wed Jul 10 08:15:33 UTC 2019


On Wednesday, 10 July 2019 at 08:03:30 UTC, Nathan S. wrote:
> I want to be able to do things like:
>
> ---
> bool isSame(Object a, Object b) { return a is b; }
>
> interface SomeInterface { int whatever(); }
>
> bool failsToCompile(SomeInterface a, SomeInterface b) { return 
> isSame(a, b); }
> ---
>
>>Error: function isSame(Object a, Object b) is not callable 
>>using argument types (SomeInterface, SomeInterface)
>
> Is there a way to declare an interface as explicitly not a COM 
> interface or a C++ interface? Having to add "cast(Object)" 
> everywhere is annoying.

Hi, not tested extensively but :

---
interface Foo
{
     final Object asObject()
     {
         return cast(Object) this;
     }

     alias asObject this;
}

class Bar : Foo
{

}

void main(string[] args)
{
     Foo foo = new Bar;
     Object o = foo;
     writeln(o);
}
---


More information about the Digitalmars-d-learn mailing list