Re: Storing interfaces as void[]

2021-03-13 Thread tsbockman via Digitalmars-d-learn
On Saturday, 13 March 2021 at 15:44:53 UTC, frame wrote: Maybe I don't get this right but why you don't just use the void[] as it is? void[] mem = [i]; //... return (cast(T[]) mem)[0]; This is how I exchange variable data between DLLs. That works, and is more elegant than the OP's solution.

Re: Storing interfaces as void[]

2021-03-13 Thread frame via Digitalmars-d-learn
On Friday, 12 March 2021 at 17:37:43 UTC, David Zhang wrote: I want to store interfaces as untyped void[], then cast them back to the interface at a later time. However, it appears to produce garbage values on get(). Is this even possible, and if so, what is happening here? The alternative

Re: Storing interfaces as void[]

2021-03-12 Thread tsbockman via Digitalmars-d-learn
On Saturday, 13 March 2021 at 00:36:37 UTC, David Zhang wrote: On Friday, 12 March 2021 at 22:18:59 UTC, tsbockman wrote: You can use TypeInfo references as the keys for the struct types, too. It's better than hashes because the TypeInfo is already being generated anyway, and is guaranteed to

Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
On Friday, 12 March 2021 at 22:18:59 UTC, tsbockman wrote: Why do you think you need a `void[]` slice? I think `void*` pointers are sufficient. This handles all normal data types, as long as they are allocated on the GC heap: I wanted to have the registry own the structs' memory, though

Re: Storing interfaces as void[]

2021-03-12 Thread tsbockman via Digitalmars-d-learn
On Friday, 12 March 2021 at 19:24:17 UTC, David Zhang wrote: On Friday, 12 March 2021 at 18:50:26 UTC, tsbockman wrote: The idea is to implement a service locator s.t. code like this is possible: ... I don't really need to copy or move the class instances here, just be able to read,

Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
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

Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
On Friday, 12 March 2021 at 18:50:26 UTC, tsbockman wrote: The idea is to implement a service locator s.t. code like this is possible: // struct (I didn't mention this in the top post, my mistake) auto log = Logger() api_registry.register!Logger(log); // class/interface

Re: Storing interfaces as void[]

2021-03-12 Thread tsbockman via Digitalmars-d-learn
On Friday, 12 March 2021 at 18:50:26 UTC, tsbockman wrote: /* This will return `null` if the value isn't really a reference to an instance of a subtype of `I`, or if the key isn't in the map yet. If you don't care about the latter case, it can be shortened to just `cast(I)

Re: Storing interfaces as void[]

2021-03-12 Thread tsbockman via Digitalmars-d-learn
On Friday, 12 March 2021 at 17:37:43 UTC, David Zhang wrote: I want to store interfaces as untyped void[], then cast them back to the interface at a later time. Assuming these interfaces are actual D `interface`s, declared with the keyword and using the default `extern(D)` linkage, you're

Re: Storing interfaces as void[]

2021-03-12 Thread Imperatorn via Digitalmars-d-learn
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

Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
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: I want to store interfaces as untyped void[], then cast them back to the interface at a later time. However, it appears to produce garbage values on get(). Is this even

Re: Storing interfaces as void[]

2021-03-12 Thread Imperatorn via Digitalmars-d-learn
On Friday, 12 March 2021 at 17:37:43 UTC, David Zhang wrote: I want to store interfaces as untyped void[], then cast them back to the interface at a later time. However, it appears to produce garbage values on get(). Is this even possible, and if so, what is happening here? The alternative

Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
I want to store interfaces as untyped void[], then cast them back to the interface at a later time. However, it appears to produce garbage values on get(). Is this even possible, and if so, what is happening here? The alternative would be a struct { CheckedPtr self; api_fns } e.g. void