Storing interfaces as void[]

David Zhang straivers98 at gmail.com
Fri Mar 12 19:28:13 UTC 2021


On Friday, 12 March 2021 at 18:14:12 UTC, Imperatorn wrote:
> On Friday, 12 March 2021 at 17:57:06 UTC, David  Zhang wrote:
>> On Friday, 12 March 2021 at 17:46:22 UTC, Imperatorn wrote:
>>> On Friday, 12 March 2021 at 17:37:43 UTC, David  Zhang wrote:
>>>> [...]
>>>
>>> Have you tried using Variant or jsvar 
>>> (https://code.dlang.org/packages/arsd-official%3Ajsvar)? 🤔
>>
>> It doesn't appear to support interfaces, see opAssign at line 
>> 682.
>>
>> It occurs to me that I.sizeof == 8 which is just enough for 
>> the vtbl, but not enough for an implementation ptr. Maybe it's 
>> a pointer to a {self, vtbl} pair? SomeClass.sizeof == 16, 
>> which is enough storage for both...
>
> Did you try Variant? Seems to work for me

It seems a bit overkill for this. I also want to be able to track 
memory usage here, and Variant doesn't appear able to store 
arbitrary-sized structs without untrackable allocations. The idea 
has merit, but doesn't give me the control I desire :)


More information about the Digitalmars-d-learn mailing list