#Adding generic Type struct into Enum without having to introduce a generic Type parameter

10 messages · Page 1 of 1 (latest)

bitter prism
#

So effentially there's no need to introduce the generic type parameter T in the Value enum
Yes there is, because you'd need to specify the Object to contain some type that includes that generic parameter

#

a Value<String> holds an Object<String> in the Obj variant, and Value<Foo> holds an Object<Foo> in the Obj variant

lunar swallow
#

The thing is implementing it with a generic type parameter absolutely destroys my VM. My VM has a stack that holds a Vec<Value> introducing a generic would mean i have Vec<Value<T>> which would limit me in the things i can push to the stack

bitter prism
#

if you want to use the same Value type, regardless of the inner object type, then make it a trait object, not a generic

lunar swallow
#

How would i go about this?

#

I store the information in traits?

bitter prism
#

Box<dyn SomeTrait>

lunar swallow
#

Yes but I dont quite see how I can dodge the generic type parameter by introducing a trait. Like How can I encode the information of my ObjectString into a trait?

bitter prism
#

you use a trait object instead of the generic parameter
struct Foo<T: SomeTrait>(T) vs struct Foo(Box<dyn SomeTrait>)

lunar swallow
#

Hey, thank you! I'll try to implement it!