Hi everybody!
I understand that D's classes solve some particular problems. However, they also cause problems because they cannot be copied when scoped. So how do I get virtual methods on a copyable stack object?
Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
May 11, 2022 Virtual methods on stack objects | ||||
---|---|---|---|---|
| ||||
Hi everybody! I understand that D's classes solve some particular problems. However, they also cause problems because they cannot be copied when scoped. So how do I get virtual methods on a copyable stack object? |
May 11, 2022 Re: Virtual methods on stack objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marvin Hannott | On 5/11/22 06:57, Marvin Hannott wrote: > I understand that D's classes solve some particular problems. However, > they also cause problems because they cannot be copied when scoped. So > how do I get virtual methods on a copyable stack object? Problems classes solve don't usually need copying nor need to be on the stack. Is that really necessary? If so, I can think of two solutions: a) Use a struct but provide "virtual" functions manually (with function pointers like one would do in C, all of which potentially collected in a per-type function table (a.k.a. vtbl :) )). b) Use classes but provide a virtual function that you call to copy. Ali |
May 11, 2022 Re: Virtual methods on stack objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 11 May 2022 at 14:44:59 UTC, Ali Çehreli wrote: >On 5/11/22 06:57, Marvin Hannott wrote: >I understand that D's classes solve some particular problems. Problems classes solve don't usually need copying nor need to be on the stack. Is that really necessary? If so, I can think of two solutions: a) Use a struct but provide "virtual" functions manually (with function pointers like one would do in C, all of which potentially collected in a per-type function table (a.k.a. vtbl :) )). b) Use classes but provide a virtual function that you call to copy. Ali I appreciate the answer, don't much like the "solutions". It's not so much about copying, but about heap allocations. Tying virtual methods to heap allocation is kind of unfortunate. And I am not really keen on building a poor man's vtbl. |
May 11, 2022 Re: Virtual methods on stack objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marvin Hannott | On 5/11/22 13:06, Marvin Hannott wrote: > I appreciate the answer, don't much like the "solutions". Me neither. :) > It's not so much about copying Great! > scoped class objects should have value semantics. std.typecons.scoped does exactly that: https://dlang.org/phobos/std_typecons.html#scoped import std.stdio; import std.typecons; interface Animal { string sing(); } class Cat : Animal { this() { writeln("Hi!"); } ~this() { writeln("Bye!"); } string sing() { return "mew"; } } void foo(Animal animal) { writeln(animal.sing()); } void bar() { auto cat = scoped!Cat(); foo(cat); } void main() { bar(); } The output: Hi! mew Bye! Ali |
May 11, 2022 Re: Virtual methods on stack objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 11 May 2022 at 20:23:07 UTC, Ali Çehreli wrote: >On 5/11/22 13:06, Marvin Hannott wrote: >I appreciate the answer, don't much like the "solutions". Me neither. :) >It's not so much about copying Great! >scoped class objects should have value semantics. std.typecons.scoped does exactly that: https://dlang.org/phobos/std_typecons.html#scoped import std.stdio; interface Animal { class Cat : Animal { ~this() { string sing() { void foo(Animal animal) { void bar() { void main() { The output: Hi! Ali Yeah, but you can't return It's illegal to move a class instance even if you are sure there are no pointers to it. As such, it is illegal to move a scoped object. That's kinda very limiting. Anyway, I cooked up another idea based on your first suggestions.
I mean, this is a super dumb example, but it kinda works. And I think it could be made a lot less tedious with some mixin magic. |
May 12, 2022 Re: Virtual methods on stack objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marvin Hannott | On Wednesday, 11 May 2022 at 20:53:21 UTC, Marvin Hannott wrote: >On Wednesday, 11 May 2022 at 20:23:07 UTC, Ali Çehreli wrote: >[...] Yeah, but you can't return [...] That's kinda very limiting. Anyway, I cooked up another idea based on your first suggestions.
I mean, this is a super dumb example, but it kinda works. And I think it could be made a lot less tedious with some mixin magic. add a single
outputs:
|
May 12, 2022 Re: Virtual methods on stack objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marvin Hannott | On Wednesday, 11 May 2022 at 20:53:21 UTC, Marvin Hannott wrote: >On Wednesday, 11 May 2022 at 20:23:07 UTC, Ali Çehreli wrote: >On 5/11/22 13:06, Marvin Hannott wrote: >I appreciate the answer, don't much like the "solutions". Me neither. :) >It's not so much about copying Great! >scoped class objects should have value semantics. std.typecons.scoped does exactly that: https://dlang.org/phobos/std_typecons.html#scoped import std.stdio; interface Animal { class Cat : Animal { ~this() { string sing() { void foo(Animal animal) { void bar() { void main() { The output: Hi! Ali Yeah, but you can't return It's illegal to move a class instance even if you are sure there are no pointers to it. As such, it is illegal to move a scoped object. That's kinda very limiting. Anyway, I cooked up another idea based on your first suggestions.
I mean, this is a super dumb example, but it kinda works. And I think it could be made a lot less tedious with some mixin magic.
|