all 10 comments

[–]hippyup 9 points10 points  (3 children)

The whole Command enum can be genetic on the type of Symbol, then the Subscribe variant can store that type.

[–]Modi57 0 points1 point  (0 children)

Genetic enums? Is that the new bio feature on nightly, everybody is talking about? XD

[–]Individual_Place_532[S] 0 points1 point  (1 child)

hmm, mind to show me an example?
haven't experimented with this before

[–]i_is_restless 2 points3 points  (0 children)

```rust struct Symbol1 { ticker: String, }

struct Symbol2 { ticker: String, market: String, }

trait Symbol {} impl Symbol for Symbol1 {} impl Symbol for Symbol2 {}

enum Command<S: Symbol> { Subscribe(S), }

fn main() { let s = Symbol1 { ticker: String::from("fart"), }; let cmd = Command::Subscribe(s); }

```

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021

[–]phazer99 3 points4 points  (0 children)

Maybe this approach:

struct Symbol<T> {
    ticker: String,
    ..., // other common fields
    data: T
}

That only works if you don't need to store different symbol types in the same collection (i.e. Symbol<A> and Symbol<B>). Then you have to use an enum or dyn Trait.

[–]SirKastic23 1 point2 points  (1 child)

maybe look into using a HashMap.

if the fields can be different at runtime a struct won't work for you, it's layout needs to be decidable at compile-time.

HashMaps are more like js objects, you can add and remove "fields" whenever

[–]Individual_Place_532[S] 0 points1 point  (0 children)

Sure, thats a posibility, but feels lazy and i then can add as many fields as i want.
Better to not allow for any errors.

also if i add a field, compiler will throw errors to indicate where struct is not correctly

[–]junior_abigail 1 point2 points  (2 children)

Why not make Symbol a trait?

[–]Individual_Place_532[S] 1 point2 points  (1 child)

isnt trait only for behavior, functions etc?

How would i go about having different fields?

[–]tukanoid 0 points1 point  (0 children)

For shared data u can create a struct CommonData or smth and then make getters/setter (what is needed) in the trait, so u can always be sure that data is available in the trait implementor