www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - fix struct API with an interface

reply "Flamaros" <flamaros.xavier gmail.com> writes:
I add directx 9 support on DQuick and as some of renderer objects 
are declared as struct, it seems it can make them derives from an 
interface.

Need I use final class instead to avoid virtual methods?

PS: I am not planning to support run-time switch between OpenGL 
and directX renderers.
Mar 06 2014
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 6 March 2014 at 13:26:27 UTC, Flamaros wrote:
 I add directx 9 support on DQuick and as some of renderer 
 objects are declared as struct, it seems it can make them 
 derives from an interface.

 Need I use final class instead to avoid virtual methods?

 PS: I am not planning to support run-time switch between OpenGL 
 and directX renderers.
I'm not sure I understand the question, but here's some facts that might help: struct methods are never virtual. final class methods are never virtual. structs do not support inheritance. Is the indirection caused by using an interface+class going to be a performance problem?
Mar 06 2014
parent reply "Flamaros" <flamaros.xavier gmail.com> writes:
On Thursday, 6 March 2014 at 13:35:13 UTC, John Colvin wrote:
 On Thursday, 6 March 2014 at 13:26:27 UTC, Flamaros wrote:
 I add directx 9 support on DQuick and as some of renderer 
 objects are declared as struct, it seems it can make them 
 derives from an interface.

 Need I use final class instead to avoid virtual methods?

 PS: I am not planning to support run-time switch between 
 OpenGL and directX renderers.
I'm not sure I understand the question, but here's some facts that might help: struct methods are never virtual. final class methods are never virtual. structs do not support inheritance. Is the indirection caused by using an interface+class going to be a performance problem?
Ok, it's like I though final class and struct are equivalent when calling a method (except the pointer deference, but it's minor I think). I don't think there is a real performance problem for us, it's more about to learn how to have a clean design.
Mar 06 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 6 March 2014 at 14:28:13 UTC, Flamaros wrote:
 Ok, it's like I though final class and struct are equivalent 
 when calling a method (except the pointer deference, but it's 
 minor I think).

 I don't think there is a real performance problem for us, it's 
 more about to learn how to have a clean design.
They are equivalent when calling directly through class instance. When called though interface pointer, final class still results in vtable dispatch (because type system can't know stored class is final) But if you need to inherit from existing interface it is best thing you have anyway.
Mar 06 2014
parent Xavier Bigand <flamaros.xavier gmail.com> writes:
Le 06/03/2014 19:08, Dicebot a écrit :
 On Thursday, 6 March 2014 at 14:28:13 UTC, Flamaros wrote:
 Ok, it's like I though final class and struct are equivalent when
 calling a method (except the pointer deference, but it's minor I think).

 I don't think there is a real performance problem for us, it's more
 about to learn how to have a clean design.
They are equivalent when calling directly through class instance. When called though interface pointer, final class still results in vtable dispatch (because type system can't know stored class is final) But if you need to inherit from existing interface it is best thing you have anyway.
In my case as I don't build openGL and DirectX module in the same binary, it can share the same modules and classes names. So I'll use them through the class instead of the interface. Thank you for your explanations.
Mar 06 2014