www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there any way to define an interface that can implicitly convert to

reply Nathan S. <no.public.email example.com> writes:
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.
Jul 10 2019
parent Basile B. <b2.temp gmx.com> writes:
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); } ---
Jul 10 2019