www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can change vtbl record at runtime ?

reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
Reason:
     Reuse component,
     bind custom callback without creating new class.

Concept example:
     class SaveFilePopup
     {
         void onSuccess() { /* default operations */ }
     }

     auto saveFile = new SaveFilePopup();
     saveFile.onSuccess = { /* New operations */ }

Delegate:
     may be... but, for speed reason, is possible to set the 
default code at compile-time ?

     class X
     {
        void delegate() onSuccess = { /* default code */ };
     }

Context:
     GUI, components, callbacks

Possible to change the vtbl record at runtime ?
Has functional for update vtbl records ?
Feb 02 2021
next sibling parent reply Max Haughton <maxhaton gmail.com> writes:
On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
wrote:
 Reason:
     Reuse component,
     bind custom callback without creating new class.

 Concept example:
     class SaveFilePopup
     {
         void onSuccess() { /* default operations */ }
     }

     auto saveFile = new SaveFilePopup();
     saveFile.onSuccess = { /* New operations */ }

 Delegate:
     may be... but, for speed reason, is possible to set the 
 default code at compile-time ?

     class X
     {
        void delegate() onSuccess = { /* default code */ };
     }

 Context:
     GUI, components, callbacks

 Possible to change the vtbl record at runtime ?
 Has functional for update vtbl records ?
Do you mean "Can I set onSuccess" at runtime? The virtual tables are relied upon by the compiler so I wouldn't play with them.
Feb 03 2021
next sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Wednesday, 3 February 2021 at 08:26:05 UTC, Max Haughton wrote:
 On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
 wrote:
 Possible to change the vtbl record at runtime ?
 Has functional for update vtbl records ?
Do you mean "Can I set onSuccess" at runtime? The virtual tables are relied upon by the compiler so I wouldn't play with them.
Not to mention that compiler can optimize away virtual calls if it can determine final type on call site.
Feb 03 2021
parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
On Wednesday, 3 February 2021 at 10:20:44 UTC, evilrat wrote:
 On Wednesday, 3 February 2021 at 08:26:05 UTC, Max Haughton 
 wrote:
 On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
 wrote:
 Possible to change the vtbl record at runtime ?
 Has functional for update vtbl records ?
Do you mean "Can I set onSuccess" at runtime? The virtual tables are relied upon by the compiler so I wouldn't play with them.
Not to mention that compiler can optimize away virtual calls if it can determine final type on call site.
evilrat, frame, thanks.
Feb 03 2021
prev sibling parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
On Wednesday, 3 February 2021 at 08:26:05 UTC, Max Haughton wrote:
 On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
 wrote:
 Do you mean "Can I set onSuccess" at runtime?
Yes.
Feb 03 2021
prev sibling parent frame <frame86 live.com> writes:
On Wednesday, 3 February 2021 at 05:30:37 UTC, Виталий Фадеев 
wrote:
 Reason:
     Reuse component,
     bind custom callback without creating new class.

 Concept example:
     class SaveFilePopup
     {
         void onSuccess() { /* default operations */ }
     }

     auto saveFile = new SaveFilePopup();
     saveFile.onSuccess = { /* New operations */ }

 Delegate:
     may be... but, for speed reason, is possible to set the 
 default code at compile-time ?

     class X
     {
        void delegate() onSuccess = { /* default code */ };
     }

 Context:
     GUI, components, callbacks

 Possible to change the vtbl record at runtime ?
 Has functional for update vtbl records ?
It is possible to change to context of a delegate: class A { int x = 10; void foo() { writeln(x); } } class B { int x = 20; } void main() { auto a = new A; auto b = new B; auto fn = &a.foo; fn(); fn.ptr = &b.__vptr; fn(); } But this is maybe not the best practice. If you just do not want to repeat yourself for identical code each class, use a mixin template for it.
Feb 03 2021