Can't read mongo result back to structs with vibe.d Bson serializer

2019-12-10 Thread tirithen via Digitalmars-d-learn
How can I cast Bson (vibe.d) to various struct types? The best would be to get rid of the switch statement as well. It works fine deserializing when casting directly collection.findOne!UserCreate(), but when I try from a Bson object I get below error directly. What to do? The error I get wh

Mix struct types in the same array

2019-12-18 Thread tirithen via Digitalmars-d-learn
I want to have a array/list/queue of instances of various struct types that represent events. How can I achieve that? With classes I would have interfaces I guess, but I want to use structs if possible as they seem less complex (and faster?). Can I wrap each struct with in another struct calle

Re: Mix struct types in the same array

2019-12-18 Thread tirithen via Digitalmars-d-learn
On Wednesday, 18 December 2019 at 22:11:10 UTC, Sebastiaan Koppe wrote: If you know all types up front you can use the Sumtype library on code.dlang.org Thanks, it's a good starting point, the best would be if I only needed to define that the struct would implement void applyTo(ref User use

Re: Mix struct types in the same array

2019-12-19 Thread tirithen via Digitalmars-d-learn
On Thursday, 19 December 2019 at 00:00:56 UTC, Paul Backus wrote: If all of the structs need to implement a particular method (or set of methods), you can use an interface and a templated adapter class to give them a common type. Here's a simple example: Thank you for the example that looks l

Re: Mix struct types in the same array

2019-12-19 Thread tirithen via Digitalmars-d-learn
On Thursday, 19 December 2019 at 00:00:56 UTC, Paul Backus wrote: interface Action { void applyTo(ref User); <-- change to applyTo(T)(ref T) } class ActionAdapter(T) : Action { T payload; this(T payload) { this.payload = payload; } override void applyTo(ref Use

Re: Mix struct types in the same array

2019-12-20 Thread tirithen via Digitalmars-d-learn
On Thursday, 19 December 2019 at 21:26:51 UTC, Paul Backus wrote: The reason this doesn't work is that interface methods are virtual, and virtual methods can't be templates. [1] If you want to have multiple kinds of entities, one option is to create an Entity interface and EntityAdapter(T) cl

non-constant expression while initializing two dim array

2020-06-07 Thread tirithen via Digitalmars-d-learn
How can I initialize my two dimensional array? When I try to run the code below I get the error: Error: non-constant expression ["user":[cast(Capability)0], "administrator":[cast(Capability)1]] Code: enum Capability { self, administer } alias Capabilities = immut

Web Assembly, struct to JavaScript Object and back

2020-06-23 Thread tirithen via Digitalmars-d-learn
I'm experimenting with generating wasm files with ldc2 but I'm having problems when trying to pass JavaScript Objects and receive them as structs and the other way around. I found this super nice getting started guide that works fine for basic types like double and so on https://wiki.dlang.or

Re: Web Assembly, struct to JavaScript Object and back

2020-06-24 Thread tirithen via Digitalmars-d-learn
On Wednesday, 24 June 2020 at 10:53:19 UTC, Sebastiaan Koppe wrote: On Tuesday, 23 June 2020 at 18:15:25 UTC, tirithen wrote: [...] Passing anything besides int/double/bool between JS and wasm is hard work. [...] Thanks for a really good explanation, passing pointers over the structs mat